Skip to content
<‐ View all posts

Top-level Async Function on Node.js

There might be times that before starting the server you have to do some initialization. If such initialization function is synchronous easy-peasy. But if it must be asynchronous you can have the following anonymous async function to be able to await inside;

// index.js

async function start() {
  // Can use `await` here


Or more compact version of it as;

// index.js

(async () => {
  // Can use `await` here

In the 2nd snippet we used anonymous lambda function and IIFE.