跳到主要内容

sleep [睡觉]

TS JS Deno

延迟执行异步功能。

延迟执行一部分 async 功能,让它入睡,返回 Promise.

typescript
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
typescript
async function sleepyWork() {
console.log("I'm going to sleep for 1 second.");
await sleep(1000);
console.log("I woke up after 1 second.");
}