Skip to content

Instantly share code, notes, and snippets.

@Hebilicious
Last active August 4, 2023 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hebilicious/315b16893cac806bfc64b490701054ac to your computer and use it in GitHub Desktop.
Save Hebilicious/315b16893cac806bfc64b490701054ac to your computer and use it in GitHub Desktop.
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