2621. Sleep
Description
GivenΒ a positive integer millis, write an asynchronous function that sleeps for millisΒ milliseconds. It can resolve any value.
Note that minor deviation from millis in the actual sleep duration is acceptable.
Β
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
Solutions
Solution 1
1 2 3 4 5 6 7 8 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |