sleep

Sleeps the execution for the specified number of milliseconds.

1.
/**
2.
* Sleeps the execution for the specified number of milliseconds.
3.
**/
4.
const sleep = (ms: number): Promise<true> => {
5.
return new Promise((resolve) => {
6.
setTimeout(() => {
7.
resolve(true);
8.
}, ms);
9.
});
10.
};
11.
12.
export default sleep;

1. Installtion

npx @jrtilak/lazykit add sleep

2. Parameters

  • ms (number)
    The number of milliseconds to sleep before resolving the promise.

3. Returns

  • Promise<true>
    Returns a promise that resolves to true after the specified delay.

4. Usage Example

The sleep function pauses execution for a specified duration, returning a Promise that resolves after the delay.

1.
import sleep from "@/utils/sleep";
2.
3.
//iife
4.
(async () => {
5.
console.log("sleeping for 1 second");
6.
await sleep(1000);
7.
console.log("done sleeping"); // This will be printed after 1 second
8.
})();