Questo contenuto viene visualizzato in inglese perché non è ancora disponibile una versione tradotta nella lingua selezionata. Aiutaci a tradurre questo articolo!
Il metodo catch() restituisce una Promise
e si occusa esclusivamente nei casi respinti. Si comporta come una chiamata Promise.prototype.then(undefined, onRejected)
.
Sintassi
p.catch(onRejected); p.catch(function(reason) { // rejection });
Parametri
- onRejected
- Una
Function
chiamata quando laPromise
viene respinta. Questa funzione richiede un parametro, la motivazione della respinta.
Descrizione
Il metodo catch
può essere utile per gestire errori nella composizione delle promise.
Esempi
Uso del metodo catch
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!" });
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. |
Browser compatibility
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 32 | 29.0 (29.0) [1] | 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) [1] | No support | No support | 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.