sleep [睡觉]
延迟执行异步功 能。
延迟执行一部分 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.");
}