Use-Cases of this Tutorial
- Know how to specify an anonymous async function.
- Know how to use the await operator immediately using an immediately-invoked async function.
Immediately-invoked / anonymous async functions are a way to use the await operator immediately.
Immediately-invoked Function Expression (IIFE), is a technique to execute a Javascript function as soon as they are created. It is good way of declaring variables and executing code without polluting the global namespace. These are also called anonymous functions as they have no defined names.
Very often it is required to use the await operator immediately (for example page load). The await can however be used only inside an async function. But instead of defining a new async function and calling it, it is better to use the IIFE pattern to create an async function that will be immediately called.
(async function() {
await someAsyncFunction();
})();
This can also be written in arrow function syntax :
(async () => {
await someAsyncFunction();
})();