Promise: Ignore Catch and Return to Chain
Asked Answered
N

3

23

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?

Novak answered 13/4, 2016 at 11:26 Comment(3)
.then has 2 arguments.. fulfill and reject.. You can deal with rejectTenuous
@RayonDabre Thanks for your answer but I'm not really sure what do you mean. Please can you show an example?Novak
It takes two functions as arguments: then(onFulFill, onReject)Displant
O
28

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'.
   });
Osric answered 13/4, 2016 at 11:37 Comment(1)
MG, I updated to so that the synchronous and asynchronous versions are in fact the same, logically, since you wrote "Here's the promise version". Np if you revert because you think being concise is more important than being exact.Oops
P
2

If you need just ignore all error in promiseA, you can just do it like that:

promiseA()  
  .catch(function(error){
      //just do nothing, returns promise resolved with undefined
  })  
  .then(promiseB)
  .then(promiseC) 

If you need to run promiseB only when error.type == 'missing', you can do that:

promiseA()       
  .catch(function(error, ignore){
     if(error.type == 'missing'){
        return promiseB.then(promiseC)  
     }
  })
Protostele answered 13/4, 2016 at 11:34 Comment(4)
The main point it that catch function also returns promise, so you can use it to implement fallback logic and chain result of itProtostele
No nested .then() calls please.Gambier
@Madara'sGhost why not?Simmons
Nested .then() are ok in certain cases, e.g. if you need to make detour through side chain on some condition. Of course, one chain is more readable and cleaner, but "No nested then" is just wrong.Griddlecake
P
0

The easiest and safest way to ignore promise errors is void that error. This approach is ESLint friendly too.

const myPromise1 =async()=>{
  throw new Error('explicitly rejected')
}
const myPromise2 =async()=>{
  console.log('passed');
}

myPromise1()
  .then(myPromise2())
  .catch(e=>void e)
Polygyny answered 11/2 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.