Created
September 16, 2017 13:07
-
-
Save jacoduplessis/e80e8e6f17b1c2a931359cf78b9ef19e to your computer and use it in GitHub Desktop.
Async Batch Processing
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
// 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