async function sleep(msec) {
return new Promise(resolve => setTimeout(resolve, msec));
}
然后你可以在任何其他异步函数中使用sleep函数,就像这样:
async function testSleep() {
console.log("Waiting for 1 second...");
await sleep(1000);
console.log("Waiting done."); // Called 1 second after the first console.log
}