Promise.allSettled 不兼容

不兼容 需要自己实现

/**
 * Promise.allSettled
 */
util.promiseAllSettled = (lists = []) => {
  let newArr = new Array(lists.length);
  let num = 0;
  return new Promise((resolve) => {
    lists.forEach((item, index) => {
      item.then(res => {
        let obj = {
          status: 'fulfilled',
          value: res
        };
        newArr[index] = obj;
        num++;
        if (num === lists.length) {
          resolve(newArr);
        }
      }, err => {
        let obj = {
          reason: err,
          status: 'rejected'
        };
        newArr[index] = obj;
        num++;
        if (num === lists.length) {
          resolve(newArr);
        }
      });
    });
  });
}

你可能感兴趣的:(前端,javascript,开发语言)