Execute batch of promises in series. Once Promise.all is done go to the next batch

后端 未结 6 1324
猫巷女王i
猫巷女王i 2021-02-19 15:57

I have an array that contains an array of promises, and each inner array could have either 4k, 2k or 500 promises.

In total there are around 60k promises and I may test

6条回答
  •  醉话见心
    2021-02-19 16:40

    @jfriend00 Just adding to your answer using async/await with reduce:

    function runPromisesInSeries(bigArray, getInfoForEveryInnerArgument) {
      try {
        return bigArray.reduce(async (acc, cItem) => {
          const results = await acc
          const data = await getInfoForEveryInnerArgument(cItem)
          results.push(data)
          return results
        }, Promise.resolve([]))
      } catch (err) {
        throw err
      }
    }
    

提交回复
热议问题