Skip to content

Instantly share code, notes, and snippets.

@guy-kdm
Last active August 21, 2017 10:31
Show Gist options
  • Save guy-kdm/70cef863f1b2ff875b534fab4b2d6099 to your computer and use it in GitHub Desktop.
Save guy-kdm/70cef863f1b2ff875b534fab4b2d6099 to your computer and use it in GitHub Desktop.
Chunked execution of async functions
/**
* Serially executes a promise returning function over an array of inputs,
* Each time waiting for {chunkSize} inputs execution to resolve before
* executing fn over the next inputs chunk.
* @param {A promise returning function} fn
* @param {Number} chunkSize
* @param {An array where each item is an input to fn} inputs
*/
function chunkedExecution(fn, chunkSize, inputs) {
const chunks = R.splitEvery(chunkSize, inputs);
return chunks.reduce(
(p, chunk) => p.then(() => Promise.all(chunk.map(fn))),
Promise.resolve()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment