sleep-utils is a utility package for handling asynchronous sleep operations in both JavaScript (JS) and TypeScript (TS) environments. It provides functions to pause the execution of code for a specified amount of time, making it useful in scenarios where you need to introduce delays or pauses in your applications.
You can install sleep-utils using npm:
npm install sleep-utils
import { sleep, sleepSeconds, sleepMinutes, sleepUntil, sleepRandom } from 'sleep-utils';
async function exampleUsage() {
await sleep(1000); // sleeps for 1 second
await sleepSeconds(2); // sleeps for 2 seconds
await sleepMinutes(5); // sleeps for 5 minutes
const futureTimestamp = Date.now() + 5000; // 5 seconds from now
await sleepUntil(futureTimestamp);
await sleepRandom(1000, 5000); // sleeps for a random time between 1 and 5 seconds
}
exampleUsage();
Delays the execution of the code for the specified number of milliseconds.
Delays the execution of the code for the specified number of seconds.
Delays the execution of the code for the specified number of minutes.
Delays the execution of the code until the specified timestamp (in milliseconds since the Unix epoch).
Delays the execution of the code for a random amount of time within the specified range.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.