0

Suppose within my controller I have a few async methods and I use $q.all to wait for all of them to complete. How do I add error handling to one of the methods in case the data retrieval fails and assure $q.all gets called with the other data and appropriate error message for the one that failed?

app.controller('Ctrl', ['$scope','$q', function($scope, $q) {

var promises = [];
var controller_data;

promises.push(assyncMethod1());
promises.push(assyncMethod2());

$q.all(promises).then(function () {
.... 

})

function assyncMethod1() {
 return $http.get("data.json").success( function(data) {
  controller_data = data; 
 });
 //on error?
}

function assyncMethod2() {
 return $http.get("data2.json").success( function(data) {
 ...
 });
 //on error?
}


}]);

I know for single promises you would use:

//basic version
promise.then(fnSuccess)
  .catch(fnFailure) //optional
  .finally(fnAlways) //optional

But how would you do this for multiple functions?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jebathon
  • 4,310
  • 14
  • 57
  • 108
  • 1
    If I understand correctly, your question is the same, or at least very similar, to this one, http://stackoverflow.com/questions/31424561/wait-until-all-es6-promises-complete-even-rejected-promises, You just need to interpret from ES6 to $q but that is pretty trivial. – Roamer-1888 Dec 29 '16 at 01:07
  • I think that question involves continuing regardless if one fails where I'm asking to continue and display which one failed – Jebathon Dec 29 '16 at 01:15
  • 1
    see if this helps http://plnkr.co/edit/l3xOVkMQlOtPXz5srkw0?p=preview – charlietfl Dec 29 '16 at 02:23
  • @charlietfl Thankyou this was what I was looking for – Jebathon Dec 29 '16 at 04:25
  • So I was right in the first place. Voted to close. – Roamer-1888 Dec 29 '16 at 04:43
  • Possible duplicate of [Wait until all ES6 promises complete, even rejected promises](https://stackoverflow.com/questions/31424561/wait-until-all-es6-promises-complete-even-rejected-promises) – Thatkookooguy Oct 31 '18 at 19:26

0 Answers0