.catch()
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.});
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.