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

后端 未结 6 1879
星月不相逢
星月不相逢 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条回答
  •  猫巷女王i
    2021-02-19 16:35

    @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
      }
    }
    

提交回复
热议问题