Created
February 18, 2024 05:13
-
-
Save mikesmullin/8ab3866cd4c639570b7d54e877cba4bb to your computer and use it in GitHub Desktop.
javascript parallel async promise races in batches
This file contains 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
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