Last active
April 20, 2017 15:55
-
-
Save reu/ad5a5d71bfe450a5b921543b739b5164 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
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; |
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
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