承诺:忽略捕获并返回链 [英] Promise: Ignore Catch and Return to Chain
本文介绍了承诺:忽略捕获并返回链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以忽略一个捕获并返回链?
Is it possible to ignore a catch and return back to the chain?
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 {
action1(); // throws
action2(); // skipped
action3(); // skipped
} catch (e) {
// can't resume
}
vs
try {
action1(); // throws
} catch (e) {
handleError(e);
}
action2(); // executes normally
action3();
这是承诺版本:
asyncActionA() // <-- fails with 'missing' reason
.catch(error => {
if(error.type == 'missing'){
return; // Makes sure the promise is resolved, so the chain continues
}
throw error; // Otherwise, rethrow to keep the Promise rejected
})
.asyncActionB(promiseB) // <-- runs
.asyncActionC(promiseC)
.catch(err => {
// Handle errors which are not of type 'missing'.
});
这篇关于承诺:忽略捕获并返回链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文