Created
October 10, 2023 08:30
-
-
Save byF/d80d942ab569b79ae6ca1769f2841046 to your computer and use it in GitHub Desktop.
limiting JS promise concurrency
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* 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