Skip to content

sleep()

Eugene Lazutkin edited this page Feb 25, 2026 · 6 revisions

Description

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.

Key features

  • Accepts both milliseconds and Date objects as input.
  • Returns a promise that resolves after the specified duration.
  • Ensures non-negative delay times.

Technical specifications

sleep is available as a named and default export.

await sleep(ms);
  • Input: ms (number | Date) - Duration in milliseconds or a Date object.
  • Output: Returns a Promise that resolves after the delay.

Usage Instructions

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);

Troubleshooting

  • Invalid Input: Ensure that the input is a valid number of milliseconds or a Date object.
  • Promise Resolution: The function returns a promise that resolves after the specified duration. Ensure that you are using await when calling the function.

See Also

Clone this wiki locally