Categories
JavaScript Answers

How to use await outside of an async function with JavaScript?

Spread the love

Sometimes, we want to use await outside of an async function with JavaScript.

In this article, we’ll look at how to use await outside of an async function with JavaScript.

How to use await outside of an async function with JavaScript?

To use await outside of an async function with JavaScript, we can call the async function right after we defined it.

For instance, we write

(async () => {
  await foo();
  // ...
})();

to define an async function.

Then we wrap it in parentheses and add () at the end to call it immediately after it’s defined.

Conclusion

To use await outside of an async function with JavaScript, we can call the async function right after we defined it.

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 *