By invoking then on each promise, you are essentially calling it twice - once for each promise individually, and once in the $q.all function that encompasses both promises. This results in each promise being called twice in total.
Solution one:
To avoid this redundancy, it is recommended to refrain from calling then on each promise. Instead, only utilize it within the $q.all function.
var promise1 = function(){
var promise1defered = $q.defer();
// Resolve promise1 here.
promise1defered.resolve();
return promise1defered.promise;
}
var promise2 = function(){
var promise2defered = $q.defer();
// Resolve promise2 here.
promise2defered.resolve();
return promise2defered.promise;
}
$q.all([promise1, promise2]).then(function () {
// Both promises are resolved at this point.
})
Solution two:
An alternative approach is to chain promises instead of using $q.all.
var promise2 = promise1.then(function(){
// Action to be taken after promise1 is completed
// Resolve promise2 here
]);
promise2.then(function(){
// Action to be taken after promise 2 is completed
// Both promise 1 and promise 2 are completed at this stage.
});