hyades
hyades

Reputation: 3170

Getting status of multiple promises executed asynchronously

I am executing multiple $http calls in parallel (async). My code logic looks something like this -

$scope.ids = [1, 2, 3, 4];
var promises = [];
for(var i in $scope.ids){
    promises.push(
        $http.get('http://0.0.0.0/t2/' + $scope.ids[i])
        );
}

$q.all(promises).then(
    function(res){
        console.log(res);
    },
    function(res){
        console.log(res);
    }
);

Now the $q.all() docs say that

Returns a single promise that will be resolved with an array/hash of values, each value corresponding to the promise at the same index/key in the promises array/hash. If any of the promises is resolved with a rejection, this resulting promise will be rejected with the same rejection value.

Now when all the promises pass, I get an expected response of an array containing 4 elements.

Success -> Object[4]

However, in the case where all fail, the res output shows only the one which failed.

Failure -> Hash

This is as expected and what the Angular Docs mention.

In my case I want to know the status of all the http request I made, even if it was a failure I require the result. Certainly $q.all() will break out the moment one of the promises fails. I want to wait till all the promises have either passed or failed and then get the results. How do I do this?

Upvotes: 4

Views: 442

Answers (1)

Nikos Paraskevopoulos
Nikos Paraskevopoulos

Reputation: 40298

An idea is to utilize the error callback of the promise to return a meaningful status:

function Failure(r) {
    this.response = r;
}

$scope.ids = [1, 2, 3, 4];
var promises = [];
for(var i in $scope.ids){
    promises.push(
        $http.get('http://0.0.0.0/t2/' + $scope.ids[i]).then(
            function(r) {
                return r;
            },
            function failure(r) {
                // this will resolve the promise successfully (so that $q.all
                // can continue), wrapping the response in an object that signals
                // the fact that there was an error
                return new Failure(r);
            }
        );
    );
}

Now $q.all() will always succeed, returning the array of results. Check each result with result[i] instanceof Failure. If this condition is true, there was a failure in that request - get the response doing result[i].response. IT may need some tweaking, but hopefully you get the point.

Upvotes: 2

Related Questions