Last active
August 4, 2023 14:51
-
-
Save Hebilicious/315b16893cac806bfc64b490701054ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function processItems ( | |
items: unknown[], | |
operation: () => Promise, | |
concurrency = 25 | |
) { | |
const errors = [] | |
let id = 0 | |
const exec = async () => { | |
if (id === items.length) return | |
const item = items[id++] | |
await operation(item).catch(error => errors.push({ item, error })) | |
return exec() | |
} | |
const workers = Array.from({ length: concurrency }, exec) | |
await Promise.all(workers) | |
return errors | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment