# 2621. Sleep ###### tags: `leetcode 30 days js challenge` `Easy` [2621. Sleep](https://leetcode.com/problems/sleep/) ### 題目描述 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` ### 解答 #### TypeScript ```typescript= async function sleep(millis: number): Promise<void> { return new Promise((resolve) => { setTimeout(() => resolve(), millis); }); } ``` > [name=Sheep][time=Mon, May 15, 2023] ### Reference [回到題目列表](https://hackmd.io/@Marsgoat/leetcode_every_day)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up