Suggest an editImprove this articleRefine the answer for “What does catch() do in promises?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`.catch()`** is a promise method that is called if the promise moves into the "rejected" state. **Key point:** it catches every error that occurred inside the promise, inside a `.then()` chain, or from an explicit `reject()`.Shown above the full answer for quick recall.Answer (EN)Image## 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 | 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()` |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.