.catch()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Jul 27, 2021Updated Sep 3, 2021
Contribute to Docs

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.
});

All contributors

Looking to contribute?

Learn JavaScript on Codecademy