如何批量执行承诺(首先解析 10,然后解析 10,依此类推)



请在下面找到我编写的代码。如您所见,有一个名为waitForPromiseChunkToBeSolved的函数,它记录等待批处理解析,解析一系列承诺,然后最终记录批处理已全部解析。(程序从main((函数开始。

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

我尝试使用第三方库,如 npmjs 包异步和 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

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

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

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

const results = [];
for(const batch of manyPromisesInChunksOfTen)
results.push(...await waitForPromiseChunkToBeResolved(batch));
return results;

相关内容

最新更新