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
promise.catch(onRejected)where:
onRejectedis a function that receives the error object.
3. Example: explicitly rejecting a promise
const promise = new Promise((resolve, reject) => {
reject("Error loading data");
});
promise.catch(error => {
console.log("Caught:", error);
});Output:
Caught: Error loading data.catch() fired because the promise was rejected (reject).
4. Example: an error in the code automatically triggers .catch()
const promise = new Promise(() => {
throw new Error("Something went wrong");
});
promise.catch(err => {
console.log("Caught the error:", err.message);
});Output:
Caught the error: Something went wrongAny error inside the executor function automatically triggers
reject.
5. Example: handling errors in a .then() chain
Promise.resolve()
.then(() => {
throw new Error("Error in then");
})
.catch(error => {
console.log("Caught:", error.message);
});Output:
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:
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.
Promise.reject("Error")
.catch(err => {
console.log("Caught:", err);
return "Continuing the work";
})
.then(result => {
console.log("Result:", result);
});Output:
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.
Promise.reject("Error 1")
.catch(err => {
console.log("Caught:", err);
throw new Error("Error 2");
})
.catch(err => {
console.log("Caught again:", err.message);
});Output:
Caught: Error 1
Caught again: Error 29. Example: .catch() in async functions
.catch() works the same as try...catch, but as a promise chain.
async function getData() {
throw new Error("Error in async");
}
getData().catch(err => {
console.log("Caught:", err.message);
});Output:
Caught: Error in async10. Example with try...catch for comparison
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
| Feature | Description |
|---|---|
| When it fires | When the promise is rejected (reject or throw) |
| Equivalent to | then(null, onRejected) |
| Returns | A new promise |
| Can recover the chain | If a value is returned from .catch() |
| Can pass the error on | If throw is used again inside .catch() |
| Works with async/await | The equivalent of try...catch |
Quick comparison:
| Situation | What to use |
|---|---|
| Handling an error in a promise chain | .catch() |
Handling errors in async/await | try...catch |
| Need to continue the chain after an error | return a value from .catch() |
| Need to pass the error further | throw inside .catch() |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.