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
/**
* @param {number} millis
* @return {Promise}
*/
async function sleep(millis) {
return new Promise(resolve => setTimeout(resolve, millis));
}
/*
async function sleep(millis) {
await setTimeout(function() {}, millis)
}
// setTimeout 함수는 Promise를 반환하지 않기 때문에 await 키워드로 처리할 수 없습니다.
*/
/**
* let t = Date.now()
* sleep(100).then(() => console.log(Date.now() - t)) // 100
*/