Skip to content

Instantly share code, notes, and snippets.

@yavorski
Created July 22, 2019 12:13
Show Gist options
  • Save yavorski/bdc494af300f305caf0cca0d7e1adec1 to your computer and use it in GitHub Desktop.
Save yavorski/bdc494af300f305caf0cca0d7e1adec1 to your computer and use it in GitHub Desktop.
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