请在下面找到我编写的代码。如您所见,有一个名为waitForPromiseChunkToBeResolved的函数,该函数记录等待要解决的批次,解析承诺,然后最终记录该批次已全部解决。 (程序从 main()函数开始。)
每批承诺都需要2秒钟(因为这是我通过setTimeout硬编码的内容,请参见 promiseTakes2000ms )。该程序目前总共需要2秒钟左右的时间,因为它并行执行所有40个诺言-但是我要实现的是它首先执行10个诺言,然后执行下一个10个诺言,依此类推。因此,该程序大约需要8秒钟的时间(一个包含40个承诺的数组执行4个块(每个10个承诺))。
我尝试使用第三方库,例如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
答案 0 :(得分:1)
您可以在此处并行执行所有块:
const waitForAllPromiseChunks = manyPromisesInChunksOfTen.map(async (batch) => {
await waitForPromiseChunkToBeResolved(batch)
})
相反,只是一个接一个地循环:
const results = [];
for(const batch of manyPromisesInChunksOfTen)
results.push(...await waitForPromiseChunkToBeResolved(batch));
return results;