Search code examples
javascriptpromisechain

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
     }
  })

Is something like this possible?


Solution

  • Here's the synchronous analogy:

    try {
      actionA(); // throws
      actionB(); // skipped
      actionC(); // skipped
    } catch (e) {
      // can't resume
    }
    

    but you want:

    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'.
    }
    

    Here's the promise version:

    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'.
       });