Wait 1 time and then all promises using $ q

I am very familiar with how it works $q, and I use it in angularjs to wait for the only promises to be resolved, and several promises to solve using $q.all().

The question is, I'm not sure if this can be done (and if it works correctly): can I wait for the only promise for a solution, but then also run some code when all my promises will be resolved too ... AFTER successful completion callbacks for user promises ... for example:

var promises = [];
for(i=1, i<5, i++){
    var singlePromise = SomeSevice.getData();
    promises.push(singlePromise);
    singlePromise.then(function(data){
         console.log("This specific promise resolved");
    });
}


// note: its important that this runs AFTER the code inside the success 
//  callback of the single promise runs ....
$q.all(promises).then(function(data){
    console.log("ALL PROMISES NOW RESOLVED"); // this code runs when all promises also resolved
});

My question is: does this work as it seems to me, or is there some strange asynchronous, vague result?

+4
1

then . . , $q.all then.

var promises = [];
for(i=1, i<5, i++){
    // singlePromise - this is now a new promise from the resulting then
    var singlePromise = SomeSevice.getData().then(function(data){
         console.log("This specific promise resolved");
    });
    promises.push(singlePromise);
}

$q.all(promises).then(function(data){
    console.log("ALL PROMISES NOW RESOLVED");
});
+6

Source: https://habr.com/ru/post/1669037/


All Articles