Sleep

Updated on 15 July, 2025
Sleep header image

Problem Statement

In this task, you are requested to develop an asynchronous function named sleep, which takes a single positive integer parameter millis. This function should perform a delay operation, pausing its execution for the duration specified by millis which represents milliseconds. Once the specified delay has elapsed, the function should complete its execution, potentially resolving with any chosen value, foundational to the implementation of timed asynchronous operations.

Examples

Example 1

Input:

millis = 100

Output:

100

Explanation:

It should return a promise that resolves after 100ms.
let t = Date.now();
sleep(100).then(() => {
console.log(Date.now() - t); // 100
});

Example 2

Input:

millis = 200

Output:

200

Explanation:

It should return a promise that resolves after 200ms.

Constraints

  • 1 <= millis <= 1000

Approach and Intuition

  1. To implement the sleep function, we need to utilize JavaScript's setTimeout function, which is ideal for creating delays.

  2. The setTimeout function allows us to specify a callback function that should be executed once a specified duration (in milliseconds) has been surpassed. Here, because our requirement is to simply wait for a specified time, this fits perfectly.

  3. Given the constraints (1 <= millis <= 1000), the function must handle milliseconds within this range effectively. Therefore, it should provide a consistent delay as specified by the input and not exceed the upper limit of 1000 milliseconds.

  4. Considering the examples provided:

    • Example 1 expects the function to pause for 100 milliseconds and subsequently resolve.
    • Example 2 expects a pause of 200 milliseconds.

    These cases illustrate the behavior of the sleep function across a spectrum of valid input values within the allowed constraints, by ensuring that the execution halts for the designated period.

  5. Since the function is asynchronous and the operation is time-based, using a function returning a Promise is the appropriate approach. This facilitates handling of the completion of the timeout in a manner that's syntactically clean and easy to integrate within other asynchronous flows, thanks to JavaScript promises.

  6. Our function could potentially resolve any value, but for the sake of transparency in the examples provided, it resolves the actual millis value used in calling the function. This is merely illustrative and can be adapted based on specific needs.

Solutions

  • JavaScript
js
async function delay(duration) {
	await new Promise(resolve => setTimeout(resolve, duration));
}

The code snippet defines an asynchronous function named delay in JavaScript, which takes a single parameter duration. This function utilizes a JavaScript Promise to implement a sleep or delay mechanism.

  • The delay function accepts duration in milliseconds.
  • Inside the function, the await keyword is used with Promise constructor. The promise resolves after a timeout equal to the specified duration.
  • The setTimeout function is used to manage the delay, and it triggers the resolve function of the promise once the timeout completes.

This technique is useful for delaying execution in asynchronous JavaScript code, which can be helpful in situations like pausing before retrying a failed operation, or simply spacing out API requests.

Comments

No comments yet.