promise实例的catch⽅法
Promise.prototype.catch()
Promise.prototype.catch()⽅法是⽤于指定发⽣错误时的回调函数
getJSON('/posts.json').then(function(posts) {
// ...
}).catch(function(error) {
// 处理 getJSON 和前⼀个回调函数运⾏时发⽣的错误
console.log('发⽣错误!', error);
});
上边代码中,getJSON( )⽅法返回⼀个Promise对象,如果该对象状态为resolved,则会调⽤then()⽅法指定的回调函数;如果异步操作抛出错误,状态就会变为rejected,就会调⽤catch( )⽅法指定的回调函数,处理这个错误。另外,then( )⽅法指定的回调函数,如果运⾏中抛出错误,也会被catch( )⽅法捕获。
try catch的使用方法
p.then((val) => console.log('fulfilled:', val))
.catch((err) => console.log('rejected', err));
// 等同于
p.then((val) => console.log('fulfilled:', val))
.then(null, (err) => console.log("rejected:", err));
下⾯是⼀个例⼦
const promise = new Promise(function(resolve, reject) {
throw new Error('test');
});
promise.catch(function(error) {
console.log(error);
});
// Error: test
上边代码中,promise抛出⼀个错误,就被catch⽅法指定的回调函数捕获。
Promise对象的错误具有冒泡的性质,会⼀直向后传递,直到被捕获为⽌。
getJSON('/post/1.json').then(function(post) {
return getJSON(postmentURL);
}).then(function(comments) {
// some code
}).catch(function(error) {
// 处理前⾯三个Promise产⽣的错误
});
上边的代码中,⼀共有三个Promise对象:⼀个由getJSON( )产⽣,两个由then( )产⽣。它们中,任何⼀个抛出错误,都会被最后⼀个catch( )捕获。
⼀般来说,不在then()⽅法⾥⾯定义Reject状态的回调函数(即then的第⼆个参数),总是使⽤catch⽅法。
// bad
promise
.then(function(data) {
// success
}, function(err) {
// error
});
// good
promise
.then(function(data) { //cb
// success
})
.catch(function(err) {
// error
});
上边的代码,第⼆种写法优于第⼀种,理由是第⼆种写法可以捕获前⾯then⽅法执⾏中的错误。因此,建议总是使⽤catch()⽅法,⽽不是使⽤then( )⽅法的第⼆个参数。
与传统的try/catch代码块不同的是,如果没有使⽤catch()⽅法指定错误处理的回调函数,Promise对象抛出的错误不会传递到外层代码,即不会有任何反应,
const someAsyncThing = function() {
return new Promise(function(resolve, reject) {
// 下⾯⼀⾏会报错,因为x没有声明
resolve(x + 2);
});
};
someAsyncThing().then(function() {
console.log('everything is great');
});
setTimeout(() => { console.log(123) }, 2000);
// Uncaught (in promise) ReferenceError: x is not defined
// 123
上边的代码中,someAsyncThing( )函数产⽣的Promise对象,内部有语法错误,浏览器运⾏到这⼀⾏,会打印出错误提⽰,但是不会退出进程,终⽌脚本运⾏,2s之后还是会输出123。这就是说,Promise内部的错误不会影响到Promise外部的代码,通俗的说法就
是“Promise 会吃掉错误”
这个脚本放在服务器执⾏,退出码是0(即表⽰执⾏成功)。不过,Node.js有⼀个unhandleRejection事件,专门监听未捕获的reject错误,上边的脚本会触发这个事件的监听函数,可以在监听函数⾥⾯抛出错误。
<('unhandledRejection', function (err, p) {
throw err;
});
上边的代码中,unhandleRejection事件的监听函数有两个参数,第⼀个是错误对象,第⼆个是报错的Promise实例,它可以⽤来了解发⽣错误的环境信息。
注意,Node 有计划在未来废除unhandleRejection事件。如果Promise内部有未捕获的错误,会终⽌进程,并且进程的退出码不为0.
⼀般总是建议,Promise对象后⾯要跟catch⽅法,这样可以处理Promise内部发⽣的错误。catch( )⽅法返回的还是⼀个Promise对象,因此后⾯还可以接着调⽤then()⽅法。
const someAsyncThing = function() {
return new Promise(function(resolve, reject) {
// 下⾯⼀⾏会报错,因为x没有声明
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( )⽅法如下:
.catch(function(error) {
console.log('oh no', error);
})
.then(function() {
console.log('carry on');
});
// carry on
上⾯的代码因为没有报错,跳过了catch()⽅法,直接执⾏后⾯的then()⽅法。此时,要是then()⽅法⾥⾯报错,就与前⾯的catch()⽆关了。catch()⽅法之中,还能再抛出错误
someAsyncThing().then(function() {
return someOtherAsyncThing();
}).catch(function(error) {
console.log('oh no', error);
// 下⾯⼀⾏会报错,因为y没有声明
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()⽅法抛出的错误

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。