Skip to content

Instantly share code, notes, and snippets.

@jzohrab
Last active February 24, 2024 09:21
Show Gist options
  • Save jzohrab/a6701d0087edca8303ec069826ec4b14 to your computer and use it in GitHub Desktop.
Save jzohrab/a6701d0087edca8303ec069826ec4b14 to your computer and use it in GitHub Desktop.
Javascript async pool - exec async functions in a pool of fixed sized
/** Async pool
* Originally seen at https://github.com/rxaviers/async-pool/blob/master/lib/es6.js
* Simplified thanks to u/GSLint in
* https://www.reddit.com/r/learnjavascript/comments/gebobv/cant_grok_asyncpool_es6_code/
*/
/** Run asyncFunction array, poolSize at a time. */
async function asyncPool (array, poolSize) {
const result = []
const pool = []
// Promises leave the pool when they're resolved.
function leavePool (e) { pool.splice(pool.indexOf(e), 1) }
for (const item of array) {
const p = Promise.resolve(item())
result.push(p)
const e = p.then(() => leavePool(e))
pool.push(e)
if (pool.length >= poolSize)
await Promise.race(pool)
}
return Promise.all(result)
}
/** Sample async workers. */
const resolveInXSeconds = (x) => {
return new Promise((resolve) => {
console.log('starting ' + x)
setTimeout(() => resolve(x), x * 1000)
})
}
const r2 = () => resolveInXSeconds(2)
const r3 = () => resolveInXSeconds(3)
const r5 = () => resolveInXSeconds(5)
asyncPool( [r2, r3, r5, r2, r3, r2, r5, r2, r2], 5 ).
then(results => console.log(results.join()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment