Hone logo
Hone
Problems

Implementing a Sleep Function in JavaScript

The sleep function is a fundamental utility in asynchronous programming. It allows you to pause the execution of your code for a specified duration, simulating real-world delays or coordinating asynchronous operations. This challenge asks you to implement a robust and accurate sleep function in JavaScript.

Problem Description

You are tasked with creating a JavaScript function called sleep that accepts a duration in milliseconds as an argument and pauses the execution of the calling code for that duration. The function should return a Promise that resolves after the specified time. This is crucial for controlling the flow of asynchronous operations and simulating delays.

Key Requirements:

  • The function must be named sleep.
  • It must accept a single argument: duration (a number representing milliseconds).
  • It must return a Promise.
  • The Promise should resolve after the specified duration.
  • The function should not reject unless an invalid duration is provided.

Expected Behavior:

When called, sleep(duration) should pause execution until duration milliseconds have passed. After the pause, the Promise returned by sleep should resolve. Code that awaits the Promise will resume execution after the delay.

Edge Cases to Consider:

  • Invalid Duration: What should happen if duration is negative or not a number? The function should reject the Promise in this case.
  • Zero Duration: A duration of 0 should resolve the Promise immediately.
  • Large Durations: The function should handle large durations without issues.
  • Accuracy: While perfect accuracy is difficult to guarantee due to system factors, the function should strive to be as accurate as possible.

Examples

Example 1:

Input: sleep(1000)
Output: Promise {<resolved>: undefined}
Explanation: Calling sleep(1000) returns a Promise. After 1000 milliseconds (1 second), the Promise resolves.

Example 2:

async function testSleep() {
  console.log("Before sleep");
  await sleep(2000);
  console.log("After sleep");
}

testSleep();
// Expected Output:
// Before sleep (immediately)
// After sleep (after 2 seconds)

Explanation: The testSleep function demonstrates how to use sleep. The "Before sleep" message is logged immediately. The await sleep(2000) line pauses execution for 2 seconds, and then the "After sleep" message is logged.

Example 3:

Input: sleep(-100)
Output: Promise {<rejected>: Error: Duration must be a non-negative number.}
Explanation: A negative duration is invalid. The Promise is rejected with an appropriate error message.

Constraints

  • duration must be a number.
  • duration must be non-negative.
  • The function must return a Promise.
  • The Promise should resolve after the specified duration.
  • The function should reject if duration is invalid (negative or not a number).
  • The function should be reasonably accurate (within a few milliseconds).

Notes

You can use setTimeout or setInterval to implement the sleep functionality. Consider using Promise.resolve() to simplify the Promise resolution. Error handling is important to ensure the function behaves predictably with invalid inputs. Remember that JavaScript's setTimeout is not perfectly precise due to system scheduling.

Loading editor...
javascript