13

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?

1
  • 2
    For me it's not a duplicate. Thanks for answering my question. Commented Apr 13, 2015 at 14:59

2 Answers 2

33

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();
   });
Sign up to request clarification or add additional context in comments.

4 Comments

When a rejected promise has an rejection handler that promise still stays rejected since promises never transition once they resolved. The returned promise from the thenable chaining is resolved if the value returned from the promise error handler is not a rejected promise
I think we are saying the same thing. When I said "the promise is considered resolved, I was actually referring to the promise returned from the then call. However, my wording could be clearer...
Thanks for clarifying, found a duplicate but good answer now nontheless.
@pixelbits, return $q.reject() is indeed a better option than throwing, but maybe you could add this option to your answer as well so that OP if sees this technique will recognize it.
0

then/fail function returns a promise, which can be rejected by throwing errors. If you want to chain multiple error handlers and trigger them all you should throw error from preceding error handlers.

var d = $q.defer();
d.promise.catch(errorHandler1).catch(errorHandler2);
d.reject();

function errorHandler1 {
 throw new Error();
}

function errorHandler2() {
  console.log("I am triggered");
}

Or instead of catch, you can use then method and pass errorHandler1 and errorHandler2 as a second parameter.

9 Comments

This will log an error to $exceptionHandler in Angular, also I don't think this is what OP wants.
Right, I didn't know that there is such option return $q.reject();. It's much better then throwing an error. Is this also standard for Q library and not only angular's $q?
In Q it's perfectly fine to throw since the $exceptionHandler hack does not exist - although you should really not be using Q in new code anymore.
although you should really not be using Q in new code anymore - do you mean in angular code?
Yes, returning a rejected promise behaving this way is a requirement of the Promises/A+ standard. It will behave consistently across all Promises/A+ implementations.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.