Pregunta

Imagine following

var resource = resourceClass.get(function(data){ console.log("red"); console.log(data); });

resource.$promise.then(function(data){ console.log("green"); console.log(data); });

Which of these success callbacks will be executed first, red or green? Will it always be the same?

¿Fue útil?

Solución

Well. A ngResource .get call internally does:

Resource[name] = function (a1, a2, a3, a4) {
var params = {}, data, success, error;
...
var promise = $http(httpConfig).then(function (response) {
...
promise = promise.then(function (response) {
    var value = responseInterceptor(response);
    (success || noop)(value, response.headers);
     return value;
},responseErrorInterceptor)

value.$promise = promise;
return value;

This means that basically, the success callback will always run before the $promise .then. However, this is implementation detail, I would not rely on it if I were you.

The promise itself chains so you can do:

resource.$promise.then(function(data){
    console.log("green"); 
    console.log(data);
    return data;
}).then(function(data){
    console.log("foo",data);
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top