Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Promise.prototype.catch

Handles a promise's error.

Available since: ES2015

Syntax

promise.catch(onRejected)

Parameters

ParameterTypeDescription
onRejectedFunctionA function that receives the error.

Return value

PromiseA new promise — after the error is handled the chain continues.

Examples

Catching an error:

Promise.reject(new Error('Network error'))
  .catch((error) => console.log('Caught:', error.message));

Output:

Caught: Network error

One catch for the whole chain:

Promise.resolve('{bad json')
  .then((text) => JSON.parse(text))
  .then((data) => console.log('not printed', data))
  .catch((error) => console.log(error.name));

Output:

SyntaxError

Notes

An error in any then() skips the remaining then() calls and goes to the nearest catch(). A rejected promise without catch() shows an Uncaught (in promise) warning in the console.

See also

In the tutorial

  • Promises — Promises in JavaScript: promise states, then, catch, finally, creating a promise with new Promise, and chains.

Thanks for reading!

Donate to the site — we work hard for you.