image.png
代码语言:javascript复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>map函数组延时写法</title>
</head>
<body>
<script>
async function doo(){
console.log("程序开始运行");
let pl = [1, 2, 3].map(async (value, index)=>{
await new Promise((resolve, reject)=>{
setTimeout(()=>{
console.log("=距离程序开始运行已经过了=", value*value, "秒");
resolve();
}, value*value*1000)
})
});
for (let i = 0; i<pl.length; i ){
await pl[i];
}
console.log("程序结束了");
}
doo();
</script>
</body>
</html>
代码更简洁一些?
可以把最后的for循环
代码语言:javascript复制for (let i = 0; i<pl.length; i ){
await pl[i];
}
换成
代码语言:javascript复制await Promise.all(pl);
效果是一样的~