Promise.prototype.catch()
Promise.prototype.then(undefined, onRejected)
(de fato, chamando obj.catch(onRejected)
internamente é chamado obj.then(undefined, onRejected)
).Sintaxe
p.catch(onRejected); p.catch(function(motivo) { // rejeição });
Parâmetros
- onRejected
- Uma
Function
chamada quando aPromise
é rejeitada. Esta função possui um argumento:
reason da rejeição.
O motivo da rejeição.
A Promise retornada pelocatch()
é rejeitada apenas seonRejected
cospe um erro ou se o o retorno da Promise foi rejeitada por si mesmo, ou seja, foi resolvida.
Valor de retorno
Internamente chamamos Promise.prototype.then
sobre o objeto que é chamando passando parâmetros como undefined
e onRejected
no manipulador de eventos. Então retornamos o valor da chamada que é Promise
.
O exemplo abaixo está cuspindo uma string. Isso é considerado uma má prática. Sempre cuspir uma instance de erro (Error). Em todo caso, a parte que faz a captura deve fazer verificaçoes sobre os argumentos para saber se é uma string ou um erro e você poderá perder informações valiosas como stack traces.
Demonstração de uma camada interna:
// Sobrescrevendo o techo original de Promise.prototype.then/catch adicionando alguns logs
(function(Promise){
var originalThen = Promise.prototype.then;
var originalCatch = Promise.prototype.catch;
Promise.prototype.then = function(){
console.log('> > > > > > chamando .then em %o com argumentos: %o', this, arguments);
return originalThen.apply(this, arguments);
};
Promise.prototype.catch = function(){
console.log('> > > > > > chamando .catch em %o com argumentos: %o', this, arguments);
return originalCatch.apply(this, arguments);
};
})(this.Promise);
// chamando um catch em uma Promise já resolvida.
Promise.resolve().catch(function XXX(){});
// logs:
// > > > > > > chamando .catch na Promise{} com os argumentos: Arguments{1} [0: function XXX()]
// > > > > > > chamando .then na Promise{} com os argumentos: Arguments{2} [0: undefined, 1: function XXX()]
Description
O método catch
pode ser útil para manipulação de erros na composição da sua promise.
Exemplos
Usando o método catch
var p1 = new Promise(function(resolve, reject) {
resolve('Sucesso');
});
p1.then(function(value) {
console.log(value); // "Sucesso!"
throw 'Ah, não!';
}).catch(function(e) {
console.log(e); // "Ah, não!"
}).then(function(){
console.log('Após um catch, a sequencia é restaurada');
}, function () {
console.log('Não engatilhado devido ao catch');
});
// O seguinte se comporta da mesma maneira que o anterior
p1.then(function(value) {
console.log(value); // "Sucesso!"
return Promise.reject('Ah, não!');
}).catch(function(e) {
console.log(e); // "Ah, não!"
}).then(function(){
console.log('Após um catch, a sequencia é restaurada');
}, function () {
console.log('Não engatilhado devido ao catch');
});
Especificações
Especificação | Status | Comentário |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Promise.prototype.catch' in that specification. |
Padrão | Initial definition in an ECMA standard. |
ECMAScript (ECMA-262) The definition of 'Promise.prototype.catch' in that specification. |
Padrão em tempo real |
Compatibilidade dos browsers
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.