The catch() method returns a Promise
and deals with rejected cases only. It behaves the same as calling Promise.prototype.then(undefined, onRejected)
(in fact, calling obj.catch(onRejected)
internally calls obj.then(undefined, onRejected)
). This means, that you have to provide onRejected
function even if you want to fallback to undefined
result value - for example obj.catch(() => {})
.
p.catch(onRejected); p.catch(function(reason) { // rejection });
Function
called when the Promise
is rejected. This function has one argument: reason
catch()
is rejected if onRejected
throws an error or returns a Promise which is itself rejected; otherwise, it is resolved.Internally calls Promise.prototype.then
on the object upon which is called, passing the parameters undefined
and the onRejected
handler received; then returns the value of that call (which is a Promise
).
The examples below are throwing strings. This is considered bad practice. Always throw an instance of Error. Otherwise the part doing the catching would have to make checks to see if the argument was a string or an error, and you might lose valuable information like stack traces
Demonstration of the internal call:
// overriding original Promise.prototype.then/catch just to add some logs (function(Promise){ var originalThen = Promise.prototype.then; var originalCatch = Promise.prototype.catch; Promise.prototype.then = function(){ console.log('> > > > > > called .then on %o with arguments: %o', this, arguments); return originalThen.apply(this, arguments); }; Promise.prototype.catch = function(){ console.log('> > > > > > called .catch on %o with arguments: %o', this, arguments); return originalCatch.apply(this, arguments); }; })(this.Promise); // calling catch on an already resolved promise Promise.resolve().catch(function XXX(){}); // logs: // > > > > > > called .catch on Promise{} with arguments: Arguments{1} [0: function XXX()] // > > > > > > called .then on Promise{} with arguments: Arguments{2} [0: undefined, 1: function XXX()]
The catch
method can be useful for error handling in your promise composition.
catch
methodvar 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'); });
// Throwing an error will call the catch method most of the time var p1 = new Promise(function(resolve, reject) { throw 'Uh-oh!'; }); p1.catch(function(e) { console.log(e); // "Uh-oh!" }); // Errors thrown inside asynchronous functions will act like uncaught errors var p2 = new Promise(function(resolve, reject) { setTimeout(function() { throw 'Uncaught Exception!'; }, 1000); }); p2.catch(function(e) { console.log(e); // This is never called }); // Errors thrown after resolve is called will be silenced var p3 = new Promise(function(resolve, reject) { resolve(); throw 'Silenced Exception!'; }); p3.catch(function(e) { console.log(e); // This is never called });
//Create a promise which would not call onReject var p1 = Promise.resolve("calling next"); var p2 = p1.catch(function (reason) { //This is never called console.log("catch p1!"); console.log(reason); }); p2.then(function (value) { console.log("next promise's onFulfilled"); /* next promise's onFulfilled */ console.log(value); /* calling next */ }, function (reason) { console.log("next promise's onRejected"); console.log(reason); });
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 Latest Draft (ECMA-262) The definition of 'Promise.prototype.catch' in that specification. | Draft |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 32 | Yes | 29 | No | 19 | 8 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 4.4.3 | 32 | Yes | 29 | Yes | 8 | Yes |
Server | |
---|---|
Node.js | |
Basic support | 0.12 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch