{{JSRef}}
The catch() method returns a Promise
and deals with rejected cases only. It behaves the same as calling {{jsxref("Promise.then", "Promise.prototype.then(undefined, onRejected)")}}.
Syntax
p.catch(onRejected); p.catch(function(reason) { // rejection });
Parameters
- onRejected
- A {{jsxref("Function")}} called when the
Promise
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(e){ 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(e){ console.log('after a catch the chain is restored'); }, function () { console.log('Not fired due to the catch'); });
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('ES6', '#sec-promise.prototype.catch', 'Promise.prototype.catch')}} | {{Spec2('ES6')}} | Initial definition in an ECMA standard. |
{{SpecName('ESDraft', '#sec-promise.prototype.catch', 'Promise.prototype.catch')}} | {{Spec2('ESDraft')}} |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 32 | {{CompatGeckoDesktop(29.0)}} | {{CompatNo}} | 19 | 7.1 |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|
Basic support | {{CompatNo}} | {{CompatGeckoMobile(29.0)}} | {{CompatNo}} | {{CompatNo}} | 8 | 32 |
{{ EmbedCompatTable("web_javascript_reference_global_objects_promi04de7") }}
See also
- {{jsxref("Promise")}}
- {{jsxref("Promise.prototype.then()")}}