承诺:忽略捕获并返回链

Ada*_*dam 12 javascript ignore promise 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
     }
  })
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?

Mad*_*iha 18

这是同步类比:

try {
  action1(); // throws
  action2(); // skipped
  action3(); // skipped
} catch (e) {
  // can't resume
}
Run Code Online (Sandbox Code Playgroud)

VS

try {
  action1(); // throws
} catch (e) {
  handleError(e);
}
action2(); // executes normally
action3();
Run Code Online (Sandbox Code Playgroud)

这是承诺版本:

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'.
});
Run Code Online (Sandbox Code Playgroud)