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

← All JS Promise reference · async/await vs promises vs callbacks