0

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

Sajjan Sarkar
  • 3,900
  • 5
  • 40
  • 51
  • 1
    your edited code should work even without the timeout. also note, you could chain with catch: `$q.all([f1(), f2()]).then(s).catch(f);` – user2954463 May 02 '17 at 16:30

1 Answers1

1

Should be:

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

or

f2 = function() {
 return $q.reject()
}
Petr Averyanov
  • 9,327
  • 3
  • 20
  • 38