是否可以忽略捕获并返回到链?
promiseA() // <-- fails with 'missing' reason
.then(promiseB) // <-- these are not going to run
.then(promiseC)
.catch(function(error, ignore){
if(error.type == 'missing'){
ignore() // <-- ignore the catch and run promiseB and promiseC
}
})
这样的事情可能吗?
这是同步类比:
try {
actionA(); // throws
actionB(); // skipped
actionC(); // skipped
} catch (e) {
// can't resume
}
但你想要:
try{
try {
actionA(); // throws
} catch (e) {
if(error.type !== 'missing'){
throw error; // abort chain
}
// keep going
}
actionB(); // executes normally if error type was 'missing'
actionC();
} catch (e) {
// handle errors which are not of type 'missing'.
}
这是承诺版本:
asyncActionA() // <-- fails with 'missing' reason
.catch(error => {
if(error.type !== 'missing'){
throw error; // abort chain. throw is implicit Promise.reject(error);
}
// keep going, implicit: return Promise.resolve();
})
.asyncActionB(promiseB) // <-- runs
.asyncActionC(promiseC)
.catch(err => {
// handle errors which are not of type 'missing'.
});
如果你需要忽略promiseA中的所有错误,你可以这样做:
promiseA()
.catch(function(error){
//just do nothing, returns promise resolved with undefined
})
.then(promiseB)
.then(promiseC)
如果你只需要在
error.type == 'missing'
时运行promiseB,你可以这样做:
promiseA()
.catch(function(error, ignore){
if(error.type == 'missing'){
return promiseB.then(promiseC)
}
})