手写promise.all

43 阅读1分钟
Promise.myAll = function (list) {
  const results = [];
  let count = 0;
  return new Promise((resolve, reject) => {
    list.map((item, index) => {
      item.then(
        (result) => {
          results[index] = result;
          count += 1;
          if (count === list.length) {
           resolve(results);
          }
        },
        (reason) => reject(reason)
      );
    });
  });
};