Categories
JavaScript Answers

How to throw exception from async function with JavaScript?

Spread the love

Sometimes, we want to throw exception from async function with JavaScript.

In this article, we’ll look at how to throw exception from async function with JavaScript.

How to throw exception from async function with JavaScript?

To throw exception from async function with JavaScript, we can use the throw keyword.

For instance, we write:

(async () => {
  try {
    await Promise.reject('error')
  } catch (e) {
    throw e;
  }
})()

We use the throw keyword to throw the error object e.

And we’ll see the error logged in the console.

Conclusion

To throw exception from async function with JavaScript, we can use the throw keyword.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *