Skip to main content

What does catch() do in promises?

1. What .catch() does

.catch() is a promise method that is called if the promise moves into the "rejected" state. That is, it catches every error that occurred:

  • inside the promise,
  • inside a .then() chain,
  • or from an explicit reject().

2. Syntax

javascript
promise.catch(onRejected)

where:

  • onRejected is a function that receives the error object.

3. Example: explicitly rejecting a promise

javascript
const promise = new Promise((resolve, reject) => { reject("Error loading data"); }); promise.catch(error => { console.log("Caught:", error); });

Output:

javascript
Caught: Error loading data

.catch() fired because the promise was rejected (reject).

4. Example: an error in the code automatically triggers .catch()

javascript
const promise = new Promise(() => { throw new Error("Something went wrong"); }); promise.catch(err => { console.log("Caught the error:", err.message); });

Output:

javascript
Caught the error: Something went wrong

Any error inside the executor function automatically triggers reject.

5. Example: handling errors in a .then() chain

javascript
Promise.resolve() .then(() => { throw new Error("Error in then"); }) .catch(error => { console.log("Caught:", error.message); });

Output:

javascript
Caught: Error in then

.catch() intercepts errors even if they occurred in one of the earlier .then() calls.

6. .catch() = .then(null, onRejected)

These two variants are fully equivalent:

javascript
promise.catch(onRejected); promise.then(null, onRejected);

Both handle a rejected promise. But .catch() reads more simply and is used more often.

7. Example: returning a value from .catch()

If .catch() returns a value, it "recovers" the chain, and the next .then() receives that value as its result.

javascript
Promise.reject("Error") .catch(err => { console.log("Caught:", err); return "Continuing the work"; }) .then(result => { console.log("Result:", result); });

Output:

javascript
Caught: Error Result: Continuing the work

.catch() can not only catch errors, but also "heal" the chain.

8. Example: re-throwing an error

If .catch() throws again (throw), the error is passed on to the next .catch() in the chain.

javascript
Promise.reject("Error 1") .catch(err => { console.log("Caught:", err); throw new Error("Error 2"); }) .catch(err => { console.log("Caught again:", err.message); });

Output:

javascript
Caught: Error 1 Caught again: Error 2

9. Example: .catch() in async functions

.catch() works the same as try...catch, but as a promise chain.

javascript
async function getData() { throw new Error("Error in async"); } getData().catch(err => { console.log("Caught:", err.message); });

Output:

javascript
Caught: Error in async

10. Example with try...catch for comparison

javascript
async function example() { try { await Promise.reject(new Error("Error in the promise")); } catch (err) { console.log("Caught via try...catch:", err.message); } } example();

The equivalent of .catch(), but in async/await syntax.

Summary

FeatureDescription
When it firesWhen the promise is rejected (reject or throw)
Equivalent tothen(null, onRejected)
ReturnsA new promise
Can recover the chainIf a value is returned from .catch()
Can pass the error onIf throw is used again inside .catch()
Works with async/awaitThe equivalent of try...catch

Quick comparison:

SituationWhat to use
Handling an error in a promise chain.catch()
Handling errors in async/awaittry...catch
Need to continue the chain after an errorreturn a value from .catch()
Need to pass the error furtherthrow inside .catch()

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.