Sleep– Beats 100% | Optimal Solution 🚀
IntuitionThe problem requires us to create a function that pauses execution for a given number of milliseconds before resolving. The most natural approach is to
# Intuition
The problem requires us to create a function that pauses execution for a given number of milliseconds before resolving. The most natural approach is to use JavaScript's built-in `setTimeout`, which allows delaying execution.

# Approach
We use the `Promise` constructor to wrap `setTimeout`. When `setTimeout` completes after the specified time, it calls the `resolve` function, resolving the promise. This ensures that the function `sleep` can be used with `await` in an asynchronous context.

# Complexity
- Time complexity: $$O(1)$$ — The function itself executes in constant time, though it waits for the specified duration.
- Space complexity: $$O(1)$$ — The function only creates a single promise, using constant space.

# Code
```javascript
/**
 * @param {number} millis
 * @return {Promise}
 */
async function sleep(millis) {
    const result = new Promise(resolve => setTimeout(resolve, millis))
    return result
}

/** 
 * let t = Date.now()
 * sleep(100).then(() => console.log(Date.now() - t)) // 100
 */

['JavaScript']