实现sleep函数

205 阅读1分钟

在程序中加入非阻塞暂停

async function sleep(delay) { 
    return new Promise((resolve) => setTimeout(resolve, delay)); 
}

 测试

async function foo() { 
 const t0 = Date.now(); 
 await sleep(1500); // 暂停约 1500 毫秒
 console.log(Date.now() - t0); 
} 
foo();
//1502