instance
.catch()
Attaches a rejection handler to a promise chain, equivalent to .then(undefined, onRejected). Returns a new promise that fulfills with the handler's return value, letting you recover from errors.
promise.catch(onRejected) Parameters
| Parameter | Purpose |
|---|---|
| onRejected(reason) | Called with the rejection reason (typically an Error) |
| recovery | Return a value to convert rejection into fulfillment |
| rethrow | Throw inside the handler to keep the chain rejected |
Examples
fetch('/api/user')
.then(r => r.json())
.catch(err => {
console.error('Failed to load user:', err);
return { name: 'anonymous' };
}); Recovers from any upstream rejection with a default value.
loadConfig()
.catch(err => {
if (err.code === 'ENOENT') return DEFAULT_CONFIG;
throw err; // rethrow anything else
}); Handle expected errors, rethrow unknown ones.
// Silence a fire-and-forget promise safely
sendAnalytics(event).catch(() => {}); Prevents an unhandledrejection warning without doing anything with the error.
Gotcha
A .catch placed BEFORE the failing step won't catch it — put .catch at the end of the chain. Returning a value in .catch makes the next .then fire with that value, which can accidentally swallow errors.
Related
.then()
Registers callbacks for the fulfillment (and optionally rejection) of a promise and returns a new promise chained to the callback's return value. Returning a value fulfills the next promise; throwing or returning a rejected promise rejects it.
.finally()
Runs a callback after the promise settles regardless of outcome, without changing the resolved value or rejection reason. Ideal for cleanup like hiding spinners or closing connections.
try / catch with await
Since await throws when the awaited promise rejects, standard try/catch is the idiomatic way to handle async errors inside an async function. A finally block runs on both success and failure paths.
← All JS Promise reference · async/await vs promises vs callbacks