Skip to main content

Command Palette

Search for a command to run...

Sleep method to wait before executing the next code

Updated
1 min read
Sleep method to wait before executing the next code
H

Full stack developer (ReactJS, NodeJS, JavaScript, PHP, SQL)

const sleep = (delay) => {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, delay);
  });
};

async function processData() {
  console.log("Print immediately")
  await sleep(5000);
  console.log("Print after 5 seconds")
}

processData();
Output:
Print immediately

Print after 5 seconds