Sajjan Sarkar
Sajjan Sarkar

Reputation: 4198

$q.all() not calling fail handler if one promise fails

Just trying to understand how the $q.all() works:

In my example, i use $q.all() to execute 2 functions (both intentionally return reject() ), i expected the fail handler in the then() to get called, but it doesnt, why is this so?

Code:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope,$q) {


    f1 = function(){
        return $q.defer().reject();
    }

    f2 = function(){
        return $q.defer().reject();
    }
    s = function(){alert('success!'); };
    f = function(){alert('failed!');};
    $q.all([f1(),f2()]).then(s,f);
}

Fiddle:

http://jsfiddle.net/sajjansarkar/ADukg/10942/


EDIT :

I found the same code works if I make the functions return the raw promise and introduce a delay before rejecting it.

Fiddle

Upvotes: 0

Views: 147

Answers (1)

Petr Averyanov
Petr Averyanov

Reputation: 9476

Should be:

   f2 = function(){
      var p = $q.defer();
      p.reject();
      return p.promise;
    }

or

f2 = function() {
 return $q.reject()
}

Upvotes: 1

Related Questions