//这是一个定时器 promise类型的
const getTime=(seconds)=>{
return new Promise(resolve=>{
setTimeout(() => {
resolve(seconds)
}, seconds);
})
}
//创建async函数
async const test=()=>{
let arr = [getTime(2000),getTime(300),getTime(1000)]
for await (let x of arr){
console.log(x); // 2000 300 1000 按顺序的
}
}
//调用
test()
//结论 for await of 循环可以暂停循环,当第一个异步执行完成后才会执行下一个,让输出结果保持同步顺序输出。