Skip to content

Latest commit

 

History

History
74 lines (51 loc) · 1.49 KB

README_EN.md

File metadata and controls

74 lines (51 loc) · 1.49 KB
comments difficulty edit_url
true
Easy

中文文档

Description

Given a positive integer millis, write an asynchronous function that sleeps for millis milliseconds. It can resolve any value.

 

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

TypeScript

async function sleep(millis: number): Promise<void> {
    return new Promise(r => setTimeout(r, millis));
}

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