【发布时间】:2016-05-31 06:02:16
【问题描述】:
我现在想知道为什么这个 ES6 原生 Promise 设置中的 throw 没有到达 catch 块
new Promise(function(resolve,reject){
reject('bar')
}).then(function resolved(){
console.log('resolved 1');
}, function rejected(){
console.log('rejected 1')
throw new Error();
}).then(function resolved(val){
console.log('resolved 2');
}, function rejected(){
console.log('rejected 2');
}).catch(function(err){
console.log('catch');
});
我正在寻找一种将控制流传递给 catch 块的方法,但是如果我使用了一个被拒绝的处理程序,如果我抛出一个错误,控制就会在那里结束,而不是在 catch 中。
简单来说,我正在寻找一种方法来结束 catch 块,即使有一个 onRejected 处理程序......有没有办法做到这一点?
new Promise(function(resolve,reject){
throw new Error(); // this goes to onRejected
reject('bar'); // this goes to onRejected
}).then(function onResolved(){
console.log('resolved');
}, function onRejected(){
console.log('rejected')
}).catch(function(err){
console.log('catch');
});
我的目标是根据是否抛出错误与是否调用拒绝来分别分支。不确定是否可能。也许有一种方法可以显式调用 catch ? 如果可能的话,我想找到一种方法来做到这一点,而不会在最终的 onRejected 处理程序中显式抛出新错误。
这是我的目标,使用 cmets:
new Promise(function(resolve,reject){
if(success){
resolve('success'); //this goes to next onResolved
}
else if(fail){
reject('fail'); //this goes to next onRejected (or catch if there is no onRejected)
}
else {
throw new Error('Fatal'); //this goes to next catch
}
});
这就是我正在寻找的行为
【问题讨论】:
-
当您使用拒绝处理程序“处理”拒绝时,拒绝现在被视为“已处理”并且承诺状态更改为已完成,除非您从拒绝处理程序返回被拒绝的承诺或者您从拒绝处理程序。这就是 promise 拒绝处理程序的设计方式。你无法改变这一点。如果您希望不处理拒绝并继续传播,则抛出或返回拒绝的承诺。没有办法绕过这个基本的设计决策。
-
你无法区分“next catch”和“next onrejected”——
.catch(…)只是.then(null, …)的糖,其行为完全相同。如果你想分支,你应该actually branch.
标签: javascript node.js promise es6-promise