Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3. 实现Promise.allSettled #3

Open
Sunny-117 opened this issue Nov 3, 2022 · 4 comments
Open

3. 实现Promise.allSettled #3

Sunny-117 opened this issue Nov 3, 2022 · 4 comments

Comments

@Sunny-117
Copy link
Owner

/**
   * 等待所有的Promise有结果后
   * 该方法返回的Promise完成
   * 并且按照顺序将所有结果汇总
   * @param {Iterable} proms
   */
  Promise.allSettled=function(proms) {
    const ps = [];
    for (const p of proms) {
      ps.push(
        MyPromise.resolve(p).then(
          (value) => ({
            status: FULFILLED,
            value,
          }),
          (reason) => ({
            status: REJECTED,
            reason,
          })
        )
      );
    }
    return MyPromise.all(ps);
  }
@lxy-Jason
Copy link
Contributor

Promise.myAllSettled = function (proms) {
  return new Promise((resolve, reject) => {
    let settledCount = 0; //状态已经确定的promise数
    let count = 0; //promise总数
    const result = [];
    for (const prom of proms) {
      let i = count;
      count++;
      Promise.resolve(prom).then(
        (data) => {
          settledCount++;
          result[i] = {
            status: "fullfilled",
            value: data,
          };
        },
        (reason) => {
          settledCount++;
          result[i] = {
            status: "rejected",
            reason,
          };
        }
      ).finally(() => {
        if(settledCount >= count){
          resolve(result)
        }
      });
    }
  });
};

const pro = new Promise((resolve, reject) => {
  setTimeout(() => {
    reject(3);
  }, 1000);
});

Promise.allSettled([pro, Promise.resolve(1), Promise.reject(2)]).then(
  (data) => {
    console.log(data);
  }
);

Promise.myAllSettled([pro, Promise.resolve(1), Promise.reject(2)]).then(
  (data) => {
    console.log(data);
  }
);

@bearki99
Copy link

bearki99 commented Feb 8, 2023

Promise.prototype.mySettled = function (promises) {
  return new Promise((resolve) => {
    const data = [],
      len = promises.length;
    let cnt = 0;
    for (let i = 0; i < len; i++) {
      const promise = promises[i];
      Promise.resolve(promise)
        .then(
          (res) => {
            data[i] = { status: "fulfilled", value: res };
          },
          (error) => {
            data[i] = { status: "rejected", reason: error };
          }
        )
        .finally(() => {
          if (cnt === len) resolve(data);
        });
    }
  });
};
const promise1 = Promise.resolve(3);
const promise2 = new Promise((resolve, reject) =>
  setTimeout(reject, 100, "foo")
);
const promises = [promise2, promise1];
Promise.allSettled(promises).then((results) =>
  results.forEach((result) => console.log(result))
);

@kangkang123269
Copy link

function allSettled(promises) {
  return Promise.all(promises.map(p => Promise.resolve(p).then(
    value => ({ status: 'fulfilled', value }),
    reason => ({ status: 'rejected', reason })
  )));
}

@cscty
Copy link

cscty commented Jun 18, 2023

Promise.allSettled = function (ites) {
let count = 0;
let result = [];
return new Promise((resolve, reject) => {
for (let i = 0; i < ites.length; i++) {
Promise.resolve(ites[i])
.then((val) => {
count++;
result[i] = val;
if (count === ites.length) {
resolve(result);
}
})
.catch((val) => {
count++;
result[i] = val;
if (count === ites.length) {
resolve(result);
}
});
}
});
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants