node sleep
snippet in javascript

node sleep

user5652

async function init() {
  console.log(1);
  await sleep(1000);
  console.log(2);
}

function sleep(ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}   

// one liner
await new Promise(resolve => setTimeout(resolve, 5000));

nodejs promise sleep

user2842

await new Promise(r => setTimeout(r, 2000));

sleep in nodejs?

user4694

await sleep(1000)
function sleep(ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}