请在下面找到我所写的代码。如您所见,有一个名为waitForPromiseChunkToBeResolved的函数,它记录等待批处理被解析,解析一个承诺数组,然后最终记录该批处理已经全部解析。(程序从main()函数开始。)
每一批承诺都需要2秒的时间(因为这是我通过setTimeout硬编码的,参见promiseTakes2000ms)。这个程序目前总共需要大约2秒,因为它并行执行所有40个承诺,但我想要实现的是,它首先执行10个承诺,然后再执行10个承诺,以此类推。因此,程序应该花费大约8秒(由40个承诺组成的数组执行4块10项承诺)。
我试过使用第三方库,比如npmjs包、异步包和p队列。
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发布于 2019-08-17 14:28:43
在这里并行执行所有块:
const waitForAllPromiseChunks = manyPromisesInChunksOfTen.map(async (batch) => {
await waitForPromiseChunkToBeResolved(batch)
})相反,只需一个又一个地循环:
const results = [];
for(const batch of manyPromisesInChunksOfTen)
results.push(...await waitForPromiseChunkToBeResolved(batch));
return results;https://stackoverflow.com/questions/57537154
复制相似问题