codegurukool.com · Free coding tutorials & online compiler

Home › Node.js Tutorial › Node.js async and await

Node.js async and await

Work with code that takes time.

Slow work such as timers and network calls does not block Node.js. Use callbacks, promises, or async functions with await to write it in order.

Example code

function wait(ms) {
  return new Promise(function (r) { setTimeout(r, ms); });
}
async function main() {
  await wait(100);
  console.log("done");
}
main();

Run this code in the online editor

← Node.js eventsNode.js HTTP server →