用async/await 书写 Promise.all

109 阅读1分钟
    //
    function test1() {
        return new Promise((res, rej) => {
          setTimeout(() => {
            res(111);
          }, 2000);
        });
      }
      function test2() {
        return new Promise((res, rej) => {
          setTimeout(() => {
            res(2222);
          }, 3000);
        });
      }
     Promise.all([test1(), test2()]).then((res) => {
       console.log(res);
     });
        
        
    const a = async (x) => {};
    const b = async (x, y) => {};
    const c = async (x, y, z) => {};

    async function all () {
            const [resultA, resultB, resultC] = await Promise.all([
                    a(x), 
                    b(x, y),
                    c(x, y, z)
            ]);
            console.log(resultA);
            console.log(resultB);
            console.log(resultC);
    }

仅供参考