我希望a.shouldNotResolve()
将在a.cancelOrder中“捕获”被拒绝的诺言,并返回“预期要捕获”,但是它解决了,返回了“无论如何都解决了”。
const a = {
cancelOrder: function(){
return Promise.reject('something broke')
.then((x) => {
return x;
})
.catch((e) => {
console.log('this caught the error', e);
});
},
shouldNotResolve: function() {
return this.cancelOrder()
.then(() => {
console.log('promise resolved anyway');
})
.catch(() => {
console.log('expected this to catch');
});
}
}
a.shouldNotResolve(); // "promise resolved anyway"
为什么a.cancelOrder拒绝,但a.shouldNotResolve仍然可以解决?
谢谢。
最佳答案
因为它已在链的前面被捕获(“捕获”?),如cancelOrder
函数所示。如果您想将其捕获到那里,但仍将拒绝传递到链下,则需要再次将其抛出:
cancelOrder: function(){
return Promise.reject('something broke')
.then((x) => {
return x;
})
.catch((e) => {
console.log('this caught the error', e);
throw e;
});
}