异步禁止! map函数结合Promise同步写法

image.png

<!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>
异步禁止! map函数结合Promise同步写法

代码更简洁一些?

可以把最后的for循环

for (let i = 0; i<pl.length; i++){
        await pl[i];
 }

换成

await Promise.all(pl);

效果是一样的~

相关文章:

  • 2022-12-23
  • 2021-08-24
  • 2022-12-23
  • 2023-03-28
  • 2021-12-26
  • 2022-12-23
猜你喜欢
  • 2022-02-15
  • 2021-07-02
  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
  • 2021-12-20
  • 2022-12-23
相关资源
相似解决方案