.catch()

Returns a new Promise related to a previously rejected Promise in the chain. This is ideal for formatting error messages for potential Promise rejections.

Syntax

myPromiseObject.catch(rejectedPromiseCallback);

The rejectedPromiseCallback function will have access to all reject() data inside of myProjectObject.

Example

Inside the brokenPromise is a single reject() call with error information. It is then handled inside .catch().

const brokenPromise = new Promise((resolve, reject) => {
reject({
errorType: 'Unknown error',
message: 'Something went wrong.',
});
});
brokenPromise.catch((err) => {
console.log(`${err.errorType}: ${err.message}`); // Unknown error: Something went wrong.
});
Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn JavaScript on Codecademy

Edit this page on GitHub

Contributors