SOURCE

// 并发版 Promise.allSettled
const asyncPool = async (poolLimit, array, iteratorFn) => {
  const ret = [];
  const executing = [];

  for (const item of array) {
    const p = Promise.resolve().then(() => iteratorFn(item, array));
    ret.push(p);

    if (poolLimit <= array.length) {
      const e = p.finally(() => 
        executing.splice(executing.indexOf(e), 1)
      );
      executing.push(e);

      if (executing.length >= poolLimit) {
        await Promise.race(executing).catch((e) => {
          console.log('promise failed but the following will not stop ��');
        });
      }
    }
  }
  return Promise.allSettled(ret);
};

const reqest = (i) => new Promise((resolve, reject) => {
    console.log(i)
    const rand = Math.random() * 10
    setTimeout(rand > 0.6 ? resolve : reject, rand, i + 's')
})

const arr = []

const test = async () => {
    for(let i = 0; i < 100; i++){
        arr.push(i)
    }
    const res = await asyncPool(10, arr, reqest)
    console.log(res)
}
test()
console 命令行工具 X clear

                    
>
console