function sleep(interval) {
return new Promise(resolve => {
setTimeout(resolve, interval);
})
}
//用法
async function sleepTime() {
for(let i = 1; i <= 5; i++) {
console.log(i);await sleep(1000)
}}
sleepTime();
结果如下:
function sleep(interval) {
return new Promise(resolve => {
setTimeout(resolve, interval);
})
}
//用法
async function sleepTime() {
for(let i = 1; i <= 5; i++) {
console.log(i);await sleep(1000)
}}
sleepTime();
结果如下:
本文标题:2020-11-04随笔 async实现休眠
本文链接:https://www.haomeiwen.com/subject/emnwvktx.html
网友评论