The catch() method returns a Promise
and deals with rejected cases only. It behaves the same as calling Promise.prototype.then(undefined, onRejected)
.
Syntax
p.catch(onRejected); p.catch(function(reason) { // rejection });
Parameters
- onRejected
- A
Function
called when thePromise
is rejected. This function has one argument, the rejectionreason
.
Description
The catch
method can be useful for error handling in your promise composition.
Examples
Using the catch
method
var p1 = new Promise(function(resolve, reject) { resolve('Success'); }); p1.then(function(value) { console.log(value); // "Success!" throw 'oh, no!'; }).catch(function(e) { console.log(e); // "oh, no!" }).then(function(){ console.log('after a catch the chain is restored'); }, function () { console.log('Not fired due to the catch'); }); // The following behaves the same as above p1.then(function(value) { console.log(value); // "Success!" return Promise.reject('oh, no!'); }).catch(function(e) { console.log(e); // "oh, no!" }).then(function(){ console.log('after a catch the chain is restored'); }, function () { console.log('Not fired due to the catch'); });
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Promise.prototype.catch' in that specification. |
Standard | Initial definition in an ECMA standard. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Promise.prototype.catch' in that specification. |
Draft |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 32 | 29.0 (29.0) | No support | 19 | 7.1 |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|
Basic support | No support | 29.0 (29.0) | No support | No support | 8 | 32 |