Skip to content

Instantly share code, notes, and snippets.

@jacoduplessis
Created September 16, 2017 13:07
Show Gist options
  • Save jacoduplessis/e80e8e6f17b1c2a931359cf78b9ef19e to your computer and use it in GitHub Desktop.
Save jacoduplessis/e80e8e6f17b1c2a931359cf78b9ef19e to your computer and use it in GitHub Desktop.
Async Batch Processing
// just a function to simulate IO
function getDouble(n) {
const time = Math.floor(Math.random() * 10 * 1000)
return new Promise((resolve) => {
setTimeout(function() {
console.log(n*2)
resolve(n*2)
}, time)
})
}
// https://stackoverflow.com/a/41791149
function forEachPromise(items, fn) {
return items.reduce(function (promise, item) {
return promise.then(function () {
return fn(item);
});
}, Promise.resolve());
}
const a = [1,2,3,4,5,6,7,8]
const N = a.length
const batchSize = 3
const batchStarts = Array.apply(0, new Array(Math.ceil(N / batchSize))).map((x, i) => {
return i * batchSize
})
async function processBatch(start) {
const batch = a.slice(start, start + batchSize)
console.log("processing batch", batch)
return Promise.all(batch.map(async n => {
return await getDouble(n)
}))
}
;(async () => {
await forEachPromise(batchStarts, processBatch)
console.log("done")
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment