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.