Sleep method to wait before executing the next code

Table of contents

No heading

No headings in the article.

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