Skip to content

Instantly share code, notes, and snippets.

@reu
Last active April 20, 2017 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reu/ad5a5d71bfe450a5b921543b739b5164 to your computer and use it in GitHub Desktop.
Save reu/ad5a5d71bfe450a5b921543b739b5164 to your computer and use it in GitHub Desktop.
const { curry, splitEvery } = require("ramda");
const batchMap = curry((batchSize, fn, list) =>
splitEvery(batchSize, list)
.map(batch => all => Promise.all(batch.map(fn)).then(res => all.concat(res)))
.reduce((results, batch) => results.then(batch), Promise.resolve([]))
);
module.exports = batchMap;
const batchMap = require("batch-map");
const delay = interval => new Promise(res => setTimeout(res, interval));
Promise
.resolve([1, 2, 3, 4, 5, 6])
.then(batchMap(2, n => delay(1000).then(() => n * 10)))
.then(console.log);
// Will print [10, 20, 30, 40, 50, 60] after 3 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment