sleep

Sleeps the execution for the specified number of milliseconds.

1. Code

/**
 * Pauses the execution for the specified number of milliseconds.
 * @param ms - The number of milliseconds to sleep.
 * @returns A promise that resolves after the specified number of milliseconds.
 */
const sleep = (ms: number): Promise<true> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, ms);
  });
};

export default sleep;

2. Installation

npx @jrtilak/lazykit@latest add sleep -ts

3. Description

The sleep function sleeps the execution for the specified number of milliseconds.

It utilizes the setTimeout and Promise APIs to pause the execution for the specified number of milliseconds.

4. Props

Prop

Type

Default Value

ms*number---

5. Examples

import sleep from ".";

//iife
(async () => {
  console.log("sleeping for 1 second");
  await sleep(1000);
  console.log("done sleeping"); // This will be printed after 1 second
})();