How to chain promise error functions in angularjs

Galdor picture Galdor · Apr 13, 2015 · Viewed 9.6k times · Source

I know how to chain promises so that multiple success functions are executed. That is esplained in many examples. How do I chain promises so that multiple error functions are executed?

Answer

pixelbits picture pixelbits · Apr 13, 2015

When an error is handled (and, either a value is returned or no value at all), the promise returned from then is considered resolved. You have to return a rejected promise from each error handler in order to propagate and chain error handlers.

For example:

promseA.then(
   function success() {
   },
   function error() {
      return $q.reject();
   })
.promiseB.then(
   function success() {
   },
   function error() {
      return $q.reject();
   })
.promiseC.then(
   function success() {
   },
   function error() {
      return $q.reject();
   });