Thursday, June 9, 2016

Exception handling with Promises

There are some things you need to be aware of when handling exceptions in promises. If you are not careful an exception can be swallowed and the caller of the promise will never known an exception occured.

Therefore you should always take the following rules into account:

  • Add global exception handling to your promise chain by ending with a catch block.
  • Return a rejected promise if you want to maintain the exception in the promise chain.

So don’t do this:

Instead return a rejected promise in your catch block:

More information: http://odetocode.com/blogs/scott/archive/2015/10/01/javascript-promises-and-error-handling.aspx

No comments: