I´m currently working on an AngularJS controller which should work with multiple promises all returning a boolean value. Based on this boolean values a resulting boolean value will be determined. If all returned values are true also the result will be true but even if only one promise returns false also the result will be false. Currently all my service/DAO calls are chained up which causes some trouble if one promise is rejected. I guess there is a better way how to handle this situation.
Controller Code:
app.controller('PromiseController', ['$scope', 'FirstService', 'SecondService', 'ThirdService',
'FourthService', function ($scope, FirstService, SecondService, ThirdService, FourthService) {
var vm = this;
vm.statusResult = false;
vm.statusSecond = null;
vm.statusThird = null;
vm.statusFourth = null;
vm.statusFirst = null;
SecondService.getStatus()
.then(function(returnData){
vm.statusSecond = returnData;
})
.then(function(){
return ThirdService.getStatus();
})
.then(function(returnData){
vm.statusThird = returnData;
})
.then(function(){
return FourthService.getStatus();
})
.then(function(returnData){
vm.statusFourth = returnData;
})
.then(function(){
return FirstService.getStatus();
})
.then(function(returnData){
vm.statusFirst = returnData;
})
.then(function(){
if(vm.statusThird&&vm.statusFourth&&vm.statusFirst&&vm.statusSecond){
vm.statusResult = true;
}
});
return this;
}]);
So I am searching for a better way how to deal with multiple promises and how to resolve the final result of all promises. Also the app shouldn't freeze while handling the results.
EDIT
The solution with $q.all from @str works fine for the final result but I also need the individual results of all services. How am I capable to also handle the single values while also handling the final result?
$q.allorPromise.allcould run all those promises in parallel then give you an array of their results.$httpcalls to the same server