Catch Errors in a JavaScript Promise Chain with Promise.prototype.catch()

Marius Schulz
InstructorMarius Schulz

Share this video with your friends

Send Tweet

The Promise.prototype.then() method accepts two callbacks as parameters, onFulfilled and onRejected:

  • If the promise is fulfilled, onFulfilled will be called.
  • If the promise is rejected, onRejected will be called.
  • If the promise never settles (that is, stays pending forever), neither one will be called.

If you want to register a handler for rejected promises only, you can use the Promise.prototype.catch() method: .catch(onRejected) behaves the same as .then(undefined, onRejected).