--- title: Promise.prototype.catch() slug: Web/JavaScript/Reference/Global_Objects/Promise/catch translation_of: Web/JavaScript/Reference/Global_Objects/Promise/catch ---
Il metodo catch() restituisce una Promise
e si occusa esclusivamente nei casi respinti. Si comporta come una chiamata {{jsxref("Promise.then", "Promise.prototype.then(undefined, onRejected)")}}.
p.catch(onRejected); p.catch(function(reason) { // rejection });
Promise
viene respinta. Questa funzione richiede un parametro, la motivazione della respinta.Il metodo catch
può essere utile per gestire errori nella composizione delle promise.
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!" });
Specification | Status | Comment |
---|---|---|
{{SpecName('ES6', '#sec-promise.prototype.catch', 'Promise.prototype.catch')}} | {{Spec2('ES6')}} | Initial definition in an ECMA standard. |
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 32 | {{CompatGeckoDesktop(29.0)}} [1] | {{CompatNo}} | 19 | 7.1 |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|
Basic support | {{CompatNo}} | {{CompatGeckoMobile(29.0)}} [1] | {{CompatNo}} | {{CompatNo}} | 8 | 32 |
[1] Gecko 24 has an experimental implementation of Promise
, under the initial name of Future
. It got renamed to its final name in Gecko 25, but disabled by default behind the flag dom.promise.enabled
. Bug 918806 enabled Promises by default in Gecko 29.