🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

.catch() Method

AI & DATA SCIENCE // catch

.catch() handles rejected Promises. It's shorthand for .then(undefined, onRejected). Returns a new Promise.

Syntax

promise
  .then(val => processVal(val))
  .catch(err => handleError(err));

Deep Dive Course

**.catch()** is syntactic sugar for `.then(null, onRejected)`. It handles any rejection from previous steps in the chain. After `.catch()` handles an error, the chain **continues** — unless you re-throw. Placing `.catch()` in the middle of a chain lets you recover from errors.

1Understanding .catch() Method

.catch() is syntactic sugar for .then(null, onRejected). It handles any rejection from previous steps in the chain. After .catch() handles an error, the chain continues — unless you re-throw. Placing .catch() in the middle of a chain lets you recover from errors.

💡

A .catch() in the middle of a chain can recover and the chain continues. A .catch() at the end is a final fallback.

editor.html
// .catch() provides a fallback
fetch('/api/preferences')
  .then(r => r.json())
  .catch(() => ({ theme: 'light', lang: 'en' })) // fallback defaults
  .then(prefs => {
    // prefs is either the fetched data OR the fallback
    applyPreferences(prefs);
  });
localhost:3000

2Practical Example

Here is a real-world application of .catch() Method showing how it is used in production JavaScript code.

editor.html
// Multiple catch in a chain
Promise.resolve('start')
  .then(v => { throw new Error('step1 failed'); })
  .catch(e => {
    console.log('Caught:', e.message);
    return 'recovered'; // recovery value
  })
  .then(v => console.log('Chain continues with:', v));
localhost:3000

3Best Practices

Follow these guidelines when working with .catch() Method:

1. Always end Promise chains with .catch()

2. Re-throw in .catch() if you can't recover

3. Use .catch() in the middle to provide fallback values

⚠️

Tip: A .catch() in the middle of a chain can recover and the chain continues. A .catch() at the end is a final fallback.

editor.html
// .catch() provides a fallback
fetch('/api/preferences')
  .then(r => r.json())
  .catch(() => ({ theme: 'light', lang: 'en' })) // fallback defaults
  .then(prefs => {
    // prefs is either the fetched data OR the fallback
    applyPreferences(prefs);
  });
localhost:3000

Examples

Example 01Basic Usage
// .catch() provides a fallback
fetch('/api/preferences')
  .then(r => r.json())
  .catch(() => ({ theme: 'light', lang: 'en' })) // fallback defaults
  .then(prefs => {
    // prefs is either the fetched data OR the fallback
    applyPreferences(prefs);
  });
Example 02Advanced Example
// Multiple catch in a chain
Promise.resolve('start')
  .then(v => { throw new Error('step1 failed'); })
  .catch(e => {
    console.log('Caught:', e.message);
    return 'recovered'; // recovery value
  })
  .then(v => console.log('Chain continues with:', v));

Best Practices

  • Always end Promise chains with .catch()
  • Re-throw in .catch() if you can't recover
  • Use .catch() in the middle to provide fallback values

Interview Question

What is the difference between .then(null, fn) and .catch(fn)?

Hint: They're almost equivalent — but not quite.

.catch(fn) is equivalent to .then(null, fn). However, .then(onFulfilled, onRejected) does NOT catch errors thrown inside onFulfilled — both callbacks are part of the same .then() call. .then(fn).catch(errorFn) catches errors from fn as well, making it safer.

Exercises

MediumPractice using .catch() Method in a real scenario.
View Solution
// .catch() provides a fallback
fetch('/api/preferences')
  .then(r => r.json())
  .catch(() => ({ theme: 'light', lang: 'en' })) // fallback defaults
  .then(prefs => {
    // prefs is either the fetched data OR the fallback
    applyPreferences(prefs);
  });

Frequently Asked Questions

What is the difference between .then(null, fn) and .catch(fn)?

.catch(fn) is equivalent to .then(null, fn). However, .then(onFulfilled, onRejected) does NOT catch errors thrown inside onFulfilled — both callbacks are part of the same .then() call. .then(fn).catch(errorFn) catches errors from fn as well, making it safer.

Related Functions

Promisesthenawaittrycatch