Skip to content

Instantly share code, notes, and snippets.

@byF
Created October 10, 2023 08:30
Show Gist options
  • Save byF/d80d942ab569b79ae6ca1769f2841046 to your computer and use it in GitHub Desktop.
Save byF/d80d942ab569b79ae6ca1769f2841046 to your computer and use it in GitHub Desktop.
limiting JS promise concurrency
async function* mapWithLimitedConcurrency(arr, mapFn, limit = 8) {
for (let i = 0; i < arr.length; i += limit) {
yield await Promise.all(arr.slice(i, i + limit).map(mapFn));
}
}
const items = [...]
const iterator = mapWithLimitedConcurrency(items, async x => ...);
for await(const items of iterator) {
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment