美文网首页
promise async await 处理并发请求案例

promise async await 处理并发请求案例

作者: 4ANH | 来源:发表于2022-02-16 11:09 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    
    <body>
      <script>
        function getItem() {
          return new Promise((resolve, reject) => {
            setTimeout(() => {
              resolve({
                '001': "启动",
                '002': "暂停",
                '003': "故障",
                '004': "重启"
              })
            }, 2000)
          })
        }
        function getData(item) {
          return new Promise((resolve, reject) => {
            setTimeout(() => {
              resolve([
                {
                  id: 1,
                  status: "001",
                  title: "这是机器1"
                },
                {
                  id: 2,
                  status: "002",
                  title: "这是机器2"
                },
                {
                  id: 3,
                  status: "003",
                  title: "这是机器3"
                }
              ])
            },12000)
          })
        }
        async function run() {
          console.time()
          const [arr1, arr2] = await Promise.all([
            getItem(),
            getData()
          ])
          // const item = await getItem();
          // const data = await getData(item);
          arr2.map(i => {
            i.status=arr1[i.status]
          })
          console.log(arr2)
          console.timeEnd ()
        }
        run();
      </script>
    </body>
    
    </html>
    

    相关文章

      网友评论

          本文标题:promise async await 处理并发请求案例

          本文链接:https://www.haomeiwen.com/subject/bwniqrtx.html