Promise.prototype.catch()方法是.then(null,rejection)的别名,用于指定发生错误时的回调函数。
例子一(迅雷面试题)
var promise = new Promise(function (resolve,reject) {
throw new Error('test');
});
promise.catch(function (error) {
console.log(error);
});
//[Error: test]
以上代码原理:如果异步操作抛出错误,状态就会变为rejected,就会调用catch方法指定的回调函数处理这个错误。
例子二
var promise = new Promise(function (resolve,reject) {
resolve("ok");
throw new Error('test');
})
promise.then(function (value) {
console.log(value);
}).catch(function (error) {
console.log(error);
});
如果promise状态已经变成resolved,再抛出错误是无效的。
例子三
getJSON("/post/1.json").then(function (post) {
return getJSON(post.commentURL);
}).then(function (comments) {
//some code
}).catch(function (error) {
//处理前面三个promise产生的错误
});
Promise对象的错误具有冒泡的性质,会一直向后传递,直到被捕获为止。即错误总是会被下一个catch语句捕获。
上面的代码中,一共有3个promise对象,一个由getJSON产生,两个由then产生。其中任何一个抛出错误都会被最后一个catch捕获。
注意
1.catch方法返回的还是一个Promise对象,因此后面还可以接着调用then方法。
var someAsyncThing = function () {
return new Promise(function (resolve,reject) {
resolve(x+2);
});
}
someAsyncThing()
.catch(function (error) {
console.log('oh no',error);
})
.then(function () {
console.log('carry on');
});
//oh no [ReferenceError: x is not defined]
//carry on
以上代码中,运行完catch方法指定的回调函数,会接着运行后面那个then方法指定的回调函数。如果没有报错,则会跳过catch方法。
2.catch方法中还能再抛出错误。
var someAsyncThing = function () {
return new Promise(function (resolve,reject) {
resolve(x+2);
});
}
someAsyncThing().then(function () {
}).catch(function (error) {
console.log('oh no',error);
y+2;
}).then(function () {
console.log('carry on');
});
//oh no [ReferenceError: x is not defined]
以上代码中,catch方法抛出一个错误,因为后面没有别的catch方法,导致这个错误不会被捕获,也不会传递到外层。
var someAsyncThing = function () {
return new Promise(function (resolve,reject) {
resolve(x+2);
});
}
someAsyncThing().then(function () {
}).catch(function (error) {
console.log('oh no',error);
y+2;
}).catch(function (error) {
console.log('carry on',error);
});
//oh no [ReferenceError: x is not defined]
//carry on [ReferenceError: y is not defined]
以上代码中,第二个catch方法用来捕获前一个catch方法抛出的错误。
3.一般来说,不要在then方法中定义rejected状态的回调函数(即then的第二个参数),而应总是使用catch方法。
//bad
promise.then(function (data) {
//success
},function (err) {
//error
});
//good
promise.then(function (data) {
//success
}).catch(function (error) {
//error
})
以上代码中,第二种写法优于第一种,理由是前者更接近同步的写法(try/catch).