首页 > 解决方案 > 如何批量执行 Promise(先解析 10,然后再解析 10,依此类推)

问题描述

请在下面找到我编写的代码。如您所见,有一个名为 waitForPromiseChunkToBeResolved 的函数,它记录Waiting for the batch to be resolved,解析一个 promise 数组,然后最后记录下该批次已全部解决。(程序从main()函数开始。)

每批承诺应该花费 2 秒(因为这是我通过 setTimeout 硬编码的,请参阅promiseTakes2000ms)。该程序目前总共需要大约 2 秒,因为它并行执行所有 40 个承诺 - 但我想要实现的是它首先执行 10 个承诺,然后是下一个 10 个,依此类推。所以程序应该花费大约 8 秒(一个包含 40 个 promise 的数组执行 4 个块,每个块 10 个 promise)。

我尝试过使用 3rd 方库,例如 npmjs 包 async 和 p-queue。


const promiseTakes2000ms = (succeeds) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (succeeds) {
                resolve("Success / 2000ms wait")
            } else {
                reject("Failure / 2000ms wait")
            }
        }, 2000)
    })
}

const reflectPromise = (promise) => {
    return promise.then((promiseResult) => {
        return {
            promiseResult,
            success: true
        }
    }).catch((error) => {
            return {
                error,
                success: false
            }
    })
}

const sliceArrayIntoChunks = (arr, chunkSize) => {
    const chunks = []
    let i = 0
    const n = arr.length

    while (i < n) {
      chunks.push(arr.slice(i, i += chunkSize))
    }

    return chunks
}

const waitForPromiseChunkToBeResolved = (promiseChunk) => {
    console.log("=== Waiting for the batch to be resolved")
    return Promise.all(promiseChunk).then((resolvedChunkResults) => {
        console.log(resolvedChunkResults)
        console.log("*** The batch has been all resolved")
        return resolvedChunkResults
    })
}


const executePromisesBatchAfterBatch = async (promises) => {
    const promisesReflected = promises.map(reflectPromise)
    const manyPromisesInChunksOfTen = sliceArrayIntoChunks(promisesReflected, 10)
    const waitForAllPromiseChunks = manyPromisesInChunksOfTen.map(async (batch) => {
        await waitForPromiseChunkToBeResolved(batch)
    })
    await Promise.all(waitForAllPromiseChunks)
}

const main = async () => {
    const resolvingPromises = new Array(20).fill(promiseTakes2000ms(true))
    const rejectingPromises = new Array(20).fill(promiseTakes2000ms(false))
    const manyPromises = resolvingPromises.concat(rejectingPromises)
    await executePromisesBatchAfterBatch(manyPromises)
}

main()

我希望程序需要 8 秒并输出以下内容:

=== Waiting for the batch to be resolved
[ { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true } ]
*** The batch has been all resolved
=== Waiting for the batch to be resolved
[ { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true } ]
*** The batch has been all resolved
=== Waiting for the batch to be resolved
[ { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true } ]
*** The batch has been all resolved
=== Waiting for the batch to be resolved
[ { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true } ]
*** The batch has been all resolved

但是当前不正确的输出是:

=== Waiting for the batch to be resolved
=== Waiting for the batch to be resolved
=== Waiting for the batch to be resolved
=== Waiting for the batch to be resolved
[ { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true } ]
*** The batch has been all resolved
[ { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true },
  { promiseResult: 'Success / 2000ms wait', success: true } ]
*** The batch has been all resolved
[ { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false } ]
*** The batch has been all resolved
[ { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false },
  { error: 'Failure / 2000ms wait', success: false } ]
*** The batch has been all resolved

标签: javascript

解决方案


您在这里并行执行所有块:

 const waitForAllPromiseChunks = manyPromisesInChunksOfTen.map(async (batch) => {
    await waitForPromiseChunkToBeResolved(batch)
})

相反,只需一个接一个地循环:

 const results = [];

  for(const batch of manyPromisesInChunksOfTen)
    results.push(...await waitForPromiseChunkToBeResolved(batch));

 return results;

推荐阅读