Skip to content

Instantly share code, notes, and snippets.

@mikesmullin
Created February 18, 2024 05:13
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 mikesmullin/8ab3866cd4c639570b7d54e877cba4bb to your computer and use it in GitHub Desktop.
Save mikesmullin/8ab3866cd4c639570b7d54e877cba4bb to your computer and use it in GitHub Desktop.
javascript parallel async promise races in batches
const promiseBatch = async function* (concurrency, list, fn) {
for (let p = [], i = 0, l = list.length; i < l || p.length > 0;) {
if (i < l) {
let _p;
_p = fn(list[i]).then(r => [_p.__id, r]);
_p.__id = i++;
if (p.push(_p) < concurrency) {
continue;
}
}
const [id, res] = await Promise.race(p);
p = p.filter(x => x.__id !== id);
yield res;
}
};
const compileTranslationUnit = async (unit) => {
// example async work function ...
};
for await (const r of promiseBatch(CONCURRENCY, unit_files, compileTranslationUnit)) {
// r is whatever the fn returns (optional), in the order of completion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment