<分区>
使用以下代码,当我拒绝 promise 时,我看到了第一个 console.log,但我也看到了第二个 console.log。这是意料之中的,因为拒绝只会影响下一个“then()”。
问题是,在循环中有没有等价于“break”的运算符,可以跳链?快速的答案是肯定的,使用未捕获的 reject() 或 throw(),但这两种方法都会向控制台发送错误消息(它们会停止整个代码执行吗?)
那么问题来了,能不能做到“干干净净”?
*我的第一个假设是,通过捕获拒绝/错误就可以解决问题,但事实并非如此
Promise.resolve()
.then(function(){
do stuff
return Promise.resolve();
})
.then(function(){
if (a === 'this')
return Promise.resolve();
else
// is there any "break" equivalent here?
return Promise.reject();
})
.then(function(){
console.log('seen if not rejected');
},function(){
console.log('seen if rejected');
})
.then(function(){
console.log('seen all the time');
});