How to throw exception from async function with JavaScript?

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.