Notification texts go here Contact Us Buy Now!

Promise: Ignore Catch and Return to Chain

In JavaScript, promises provide a simple way to handle asynchronous operations. The .catch() method is used to catch any errors that occur during the execution of a promise. However, sometimes you may want to ignore certain errors and continue with the processing of the promise chain.

Ignoring All Errors

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

In the example above, the .catch() method is used to handle any errors that occur in the execution of promiseA(). However, the error is not re-thrown, so the promise chain continues to execute with the next .then() method, promiseB. This can be useful when you want to ignore non-critical errors.

Ignoring Specific Errors

promiseA()
  .catch(function(error, ignore){
     if(error.type == 'missing'){
        return promiseB.then(promiseC)
     }
  })

In the example above, the .catch() method is again used to handle any errors that occur in the execution of promiseA(). However, this time we use the second parameter of the .catch() method to check the type of the error. If the error type is 'missing', we return a new promise that will execute promiseB and then promiseC. This allows us to ignore the 'missing' error and continue with the processing of the promise chain.

Handling Errors

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

The code sample above is a synchronous analogy to the previous example. It shows how you can use a try/catch block to handle errors. If an error occurs in the execution of actionA(), the catch block will be executed and the execution of the remaining actions will be skipped.

Handling Errors in a Promise Chain

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

The code sample above is a promise-based version of the previous example. It shows how you can use a .catch() method to handle errors in a promise chain. If an error occurs in the execution of asyncActionA(), the .catch() method will be executed and the execution of the remaining promises in the chain will be skipped. However, if the error type is 'missing', the error will be ignored and the execution of the promise chain will continue with the next .then() method, asyncActionB().

Ignoring Errors with void

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

myPromise1()
  .then(myPromise2())
  .catch(e=>void e)

The code sample above shows how you can use the void operator to ignore errors in a promise chain. The void operator simply evaluates the expression and returns undefined. In the example above, the .catch() method is used to catch any errors that occur in the execution of myPromise1(). However, the error is not re-thrown, so the promise chain continues to execute with the next .then() method, myPromise2().

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.