Align results with source items using .useCorrespondingResults()
mainBy default, results are returned in the order they complete. Use .useCorrespondingResults() to ensure the results array matches the index of the source items.
When using this mode, the results array will contain mixed types for tasks that didn't succeed:
- The actual value (for successful tasks)
PromisePool.notRun(for tasks that were skipped)PromisePool.failed(for tasks that failed)
Note: When using corresponding results, you must handle errors yourself via .handleError() as the default error collection behavior is bypassed.
import { setTimeout } from 'node:timers/promises'
import { PromisePool } from '@supercharge/promise-pool'
const { results } = await PromisePool
.for([1, 2, 3])
.withConcurrency(5)
.useCorrespondingResults()
.process(async (number, index) => {
const value = number * 2
return await setTimeout(10 - index, value)
})
// results will be [2, 4, 6]
// To identify failed or skipped items:
const itemsNotRun = results.filter(result => result === PromisePool.notRun)
const failedItems = results.filter(result => result === PromisePool.failed)