Created
July 22, 2019 12:13
-
-
Save yavorski/bdc494af300f305caf0cca0d7e1adec1 to your computer and use it in GitHub Desktop.
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
export const parallel = (tasks, fn) => Promise.all(tasks.map(task => fn(task))); | |
export const serial = (tasks, fn) => tasks.reduce((promise, task) => promise.then(previous => fn(task, previous)), Promise.resolve(null)); | |
export const concurrent = async (tasks, fn, limit = 8) => { | |
const chunks = Array.from(Array(Math.ceil(tasks.length / limit))).map((x, i) => tasks.slice(i * limit, i * limit + limit)); | |
for (const chunk of chunks) { | |
await Promise.all(chunk.map((task) => fn(task))); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment