catch()
方法返回一个Promise
,并且只处理拒绝(rejected )的情况。它的行为与调用Promise.prototype.then(undefined, onRejected)
相同。 (事实上, calling obj.catch(onRejected)
内部调用obj.then(undefined, onRejected)
)。这意味着即使返回undefined
也要提供onRejected
函数。
内部实际上调用`Promise.prototype.then(undefined, onRejected)的演示:
// 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.error('> > > > > > 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()]
参数: onRejected 是一个函数,当Promise 被rejected时会调用这个函数,该函数有一个参数(reason,即reject的原因)。如果 onRejected 抛出一个错误 或 返回一个本身失败(rejected)的 Promise , 通过 catch() 返回的Promise 被rejected;否则,它将显示为成功(resolved)。
返回值:一个promise对象。
p.catch(onRejected);
p.catch(function(reason) {
// rejection
});
举例一:catch()方法链式操作,注意打印顺序
var p1 = new Promise(function(resolve, reject) {
resolve('Success');
});
p1.then(function(value) {
console.log(value); // "Success!"
throw new Error('oh, no!');
}).catch(function(e) {
console.error(e.message); // "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.error(e); // "oh, no!"
}).then(function(){
console.log('after a catch the chain is restored');
}, function () {
console.log('Not fired due to the catch');
});
// Success
// Success
// oh, no!
// after a catch the chain is restored
// oh, no!
// after a catch the chain is restored
举例二:捕获抛出的错误,异步(setTimeout)中的异常无法捕获到,即在异步函数中抛出的错误不会被catch捕获到
let p1 = new Promise((resolve, reject) => {
throw new Error('somethring wrong');
})
p1.catch(e => {
console.log(e); //somethring wrong
})
// Errors thrown inside asynchronous functions will act like uncaught errors
let p2 = new Promise((resolve, reject) => {
setTimeout(() => {
throw new Error('uncaught exception') // uncaught exception after 1s
}, 1000);
})
p2.catch(e => {
console.log(e); // This is never called
})
let p3 = new Promise((resolve, reject) => {
resolve();
throw new Error('silenced exception!');
})
p3.catch(e => {
console.log(e); // This is never called
})
举例三:如果已决议
let p1 = Promise.resolve('calling next');
let p2 = p1.catch(reason => {
console.log('catch p1');
console.log(reason);
})
p2.then(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);
})
其它:注意打印顺序
var p1 = new Promise(function (resolve, reject) {
resolve('Success');
});
var p2 = new Promise(function (resolve, reject) {
resolve('Success');
});
p1.then(function (value) {
console.log(value); // "Success!"
throw new Error('oh, no!');
}).catch(function (e) {
console.error(e.message); // "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
p2.then(function (value) {
console.log(value); // "Success!"
throw new Error('oh, no!');
}).catch(function (e) {
console.error(e.message); // "oh, no!"
}).then(function () {
console.log('after a catch the chain is restored');
}, function () {
console.log('Not fired due to the catch');
});
// Success
// Success
// oh, no!
// oh, no!
// after a catch the chain is restored
// after a catch the chain is restored