-
-
Notifications
You must be signed in to change notification settings - Fork 0
sleep()
Eugene Lazutkin edited this page Feb 25, 2026
·
6 revisions
The sleep() function creates a delay for a specified amount of time. It can accept either a number of milliseconds or a Date object, in which case it calculates the time remaining until that date.
- Accepts both milliseconds and
Dateobjects as input. - Returns a promise that resolves after the specified duration.
- Ensures non-negative delay times.
sleep is available as a named and default export.
await sleep(ms);-
Input:
ms(number | Date) - Duration in milliseconds or aDateobject. -
Output: Returns a
Promisethat resolves after the delay.
To use the sleep() function, simply call it with the desired duration:
import sleep from 'time-queues/sleep.js';
// Sleep for 2 seconds
await sleep(2000);
// Sleep until a specific date
const date = new Date(Date.now() + 5000); // 5 seconds from now
await sleep(date);-
Invalid Input: Ensure that the input is a valid number of milliseconds or a
Dateobject. -
Promise Resolution: The function returns a promise that resolves after the specified duration. Ensure that you are using
awaitwhen calling the function.
- Scheduler - Time-based scheduling
- random-sleep - Randomized delays
- defer() - Execute on next tick