I am currently in the process of developing an AngularJS controller that needs to work with multiple promises, each returning a boolean value. The aim is to determine a final boolean value based on these individual results. If all returned values are true
, then the result will be true
. However, if even one promise returns false
, the overall result will be false
. Currently, I have chained all my service/DAO calls together, which causes issues when one promise is rejected. I believe there must be a better way to handle this situation.
Snippet of 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;
})
// other service calls follow...
// logic for determining final status
});
return this;
}]);
Hence, I am seeking a more efficient approach to deal with multiple promises and resolve the collective outcome of all promises. Moreover, it is essential that the application remains responsive while handling these results.
UPDATE
The solution using $q.all
provided by @str works well for the final result. However, I also require the individual outcomes of each service. How can I manage both the individual values and the final result simultaneously?