AngularJs: return the promise of a nested $ http solution that is already found, but why does it work?

I want to build a nested $ http.get after the first success, then request a second.

then I went out with something like this:

$http.get('/xxx').then(function(response){
    $http.get('/yyy').then(function(response){
        //do something
    })
});

But I want to return Promise so that I can properly organize my code. Obviously, the code above does not meet my need.

Then I did a lot of research using $q.all(), but in fact with $ q.all the second request did not wait for the first one, it will send a second request, even if the first request will not be successfully answered .

After that, I found a solution that in my case works like a charm:

var promise = $http.get('/xxx').then(function(response1){
    return $http.get('/yyy').then(function(response2) {
        return response2.data;
    });;
});     
return promise;

But I do not understand why this works ???

($ http.get) then().

promise.then(function(data){
    console.log(data);
});

, , , response2.data​​strong > , ? Promise Object $http???

+4
2

.then(…) callback, .

, , .

promises; , promises:

var promise = $http.get('/xxx').then(function(response1) {
    return $http.get('/yyy');
}).then(function(response2) {
    return response2.data;
});

. .

+11

All Articles