Promise.catch

catch — A callback for a rejected Promise. Will execute as soon as the promise is rejcted. If the promise is already rejcted, it will execute instantly.

Description

Promise.catch([mixed result])

Parameters

Name Description Type Default Optional
result The result from the reject function in the promise. mixed Yes

Changelog

Version Description
ES 6 Introduced.

Examples

Example #1 – Nested example
new Promise((resolve, reject) => { reject('rejected :('); }).catch(result => { console.log(result); });
Example #2 – Hook example
let mypromise = new Promise((resolve, reject) => { reject('rejected :('); }); mypromise.catch(result => { console.log(result); });
Example #3 – Late hook example

When there is no catch hook for a rejected promise, an error is shown. With a late hook the error disappears as soon as the catch method is attached.

let mypromise = new Promise((resolve, reject) => { reject('rejected :('); }); // Nothing to catch the error, so an error message is shown. // After about 2 seconds the catch method is attached and replaces the previous error. setTimeout(() => { mypromise.catch(result => { console.log(result); }); }, 2000);

External references