Promise.allSettled 垫片

想用Promise.allSettled,但又不想升级tsconfog.json下的lib属性,可以这样模拟:

const allSettled = async (promises: Promise<any>[]) => {
  const p2 = promises.map((promise) => promise.then((value) => ({
    status: 'fulfilled',
    value,
  }), (reason) => ({
    status: 'rejected',
    reason,
  })));

  const results = await Promise.all(p2);

  return  results;
};
posted @ 2021-11-10 17:10  西河  阅读(21)  评论(0编辑  收藏  举报