Promise: Ignore Catch and Return to Chain

Adam Halasz picture Adam Halasz · Apr 13, 2016 · Viewed 19.1k times · Source

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?

Answer

Madara&#39;s Ghost picture Madara's Ghost · Apr 13, 2016

Here's the synchronous analogy:

try {
  action1(); // throws
  action2(); // skipped
  action3(); // skipped
} catch (e) {
  // can't resume
}

vs

try {
  action1(); // throws
} catch (e) {
  handleError(e);
}
action2(); // executes normally
action3();

Here's the promise version:

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