手写 promise.all

<!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>promise.all第二遍</title>
  </head>
  <body>
    <script>
      function promiseAll(_promises) {
        return new Promise((resolve, reject) => {
          const promises = [..._promises];
          const arr = [];
          let count = 1;
          for (let i = 0; i < promises.length; i++) {
            Promise.resolve(promises[i]).then(
              data => {
                arr[i] = data;
                if (++count === promises.length) {
                  resolve(arr);
                }
              },
              err => {
                reject(err);
              }
            );
          }
        });
      }
      function promiseAll1(_promises) {
        return new Promise((resolve, reject) => {
          const promises = [..._promises];
          const arr = [];
          let count = 1;
          for (let i = 0; i < promises.length; i++) {
            Promise.resolve(promises[i]).then(
              data => {
                arr[i] = data;
                if (++count === promises.length) {
                  resolve(arr);
                }
              },
              err => {
                reject(err);
              }
            );
          }
        });
      }

      function promiseAll2(_promises) {
        return new Promise((resolve, reject) => {
          const promises = [..._promises];
          let count = 1;
          const arr = [];
          for (let i = 0; i < promises.length; i++) {
            Promise.resolve(promises[i]).then(
              data => {
                arr[i] = data;
                if (++count === promises.length) {
                  resolve(arr);
                }
              },
              err => {
                reject(err);
              }
            );
          }
        });
      }

      let a = `回调函数延迟绑定;错误冒泡,返回值穿透`;
    </script>
  </body>
</html>

 

posted @ 2022-05-06 21:54  苹果π  阅读(128)  评论(0编辑  收藏  举报