sleep-utils
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.
Installation
You can install sleep-utils using npm:
npm install sleep-utils
Usage
import { sleep, sleepSeconds, sleepMinutes, sleepUntil, sleepRandom } from 'sleep-utils';
async function exampleUsage() {
await sleep(1000);
await sleepSeconds(2);
await sleepMinutes(5);
const futureTimestamp = Date.now() + 5000;
await sleepUntil(futureTimestamp);
await sleepRandom(1000, 5000);
}
exampleUsage();
API
sleep(milliseconds: number): Promise
Delays the execution of the code for the specified number of milliseconds.
sleepSeconds(seconds: number): Promise
Delays the execution of the code for the specified number of seconds.
sleepMinutes(minutes: number): Promise
Delays the execution of the code for the specified number of minutes.
sleepUntil(timestamp: number): Promise
Delays the execution of the code until the specified timestamp (in milliseconds since the Unix epoch).
sleepRandom(min: number, max: number): Promise
Delays the execution of the code for a random amount of time within the specified range.
Contributing
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.
License
MIT