怎样用一个 sleep 函数演示 async、await 的用法
index.js
function sleep(n = 0) {
return new Promise((resolve) => {
setTimeout(function () {
resolve();
}, n);
});
}
async function clock() {
while (true) {
await sleep(1000);
console.log(new Date().toLocaleTimeString());
}
}
clock();