Promise.prototype.catch()
Registers a callback for the rejection of a promise.
Syntax
promise.catch(onRejected)
Returns
Promise — A new promise; resolves with the handler's return value if it
handles the rejection.
Examples
Promise.reject(new Error("nope"))
.catch(e => console.log("handled:", e.message));
Output
handled: nope
Promise.resolve(5)
.then(() => { throw new Error("in then"); })
.catch(e => console.log(e.message));
Output
in then
Promise.reject("x")
.catch(() => "recovered")
.then(v => console.log(v));
Output
recovered
Notes
- Equivalent to `then(undefined, onRejected)`.
- A `catch` can recover by returning a value, continuing the chain.
- Place one `catch` at the end of a chain to handle all earlier errors.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 32 |
| firefox | 29 |
| safari | 8 |
| edge | 12 |
| node | 0.12 |