Last active
August 21, 2017 10:31
-
-
Save guy-kdm/70cef863f1b2ff875b534fab4b2d6099 to your computer and use it in GitHub Desktop.
Chunked execution of async functions
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
/** | |
* 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