Promiseチェーンを中断する方法
749 ワード
Promiseチェーンを中断する方法 Promiseインスタンス を作成 thenメソッド を使用 Promiseチェーンで中断する場合.thenメソッド 方法1 方法2 Promise関連サードパーティライブラリpromise-funこのようなthen()でpromiseチェーンを中断する実装は、中のp-if:Conditional promise chains を使用することができる.
const promise = new Promise(function(resolve, reject) {
// ... some code
if (/* */){
resolve(value);
} else {
reject(error);
}
});
promise
.then(res=>{
// success
})
.catch(err=>{
// failure
})
throw err
promise
.then(res=>{
if(!res){
throw err //
}
})
reject
promise
.then(res=>{
if(!res){
reject // reject
}
})