Yashua Yashua - 5 months ago 14
AngularJS Question

Running promises in parallel with Angular 1.x without stop on fail

I am fetching some images using

$http.get()
. I am calling them currently using
$q.all()
however if one call/promise fails they all do. Is there another way I could do this?

$q.all([$http.get('..'),$http.get('..')]).then(function(res) {
// this will never happen if one get fails.
}

Answer

Simple proof of concept using catch()

var req1 =  $http.get('..').catch(function(err){ return err; });
var req2 =  $http.get('..').catch(function(err){ return err; });

$q.all([req1,req2]).then(function(results) {
     var counts = {pass:0, fail:0}
     results.forEach(function(item){
       var type = item.status === 200 ? 'pass':'fail';
       counts[type]++;
     });
     alert('Results status =' + JSON.stringify(counts))
});

Because you return something from catch it resolves the initial promise and passes that return down the promise chain (to results array in this case) .

The success callback of $q.all().then will fire as a result

DEMO