Skip to content

Instantly share code, notes, and snippets.

@eriklindebratt
Last active December 13, 2021 20:28
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 eriklindebratt/21c4e1b28c75ded8f12f7fa785c53f40 to your computer and use it in GitHub Desktop.
Save eriklindebratt/21c4e1b28c75ded8f12f7fa785c53f40 to your computer and use it in GitHub Desktop.
batch-process-with-generators
import { default as _ } from "lodash";
async function* processInBatches(data, batchSize) {
const batches = _.chunk(data, batchSize);
for (const batch of batches) {
const result = await processBatch(batch);
yield {
batch,
result,
};
}
}
// simulate e.g. network request
const processBatch = (batch) => new Promise((resolve) => {
setTimeout(() => resolve({ ids: batch.map((i) => i.id) }), 2000);
});
async function main() {
const allData = [
{ id: "1" },
{ id: "2" },
{ id: "3" },
{ id: "4" },
{ id: "5" },
{ id: "6" },
{ id: "7" },
{ id: "8" },
{ id: "9" },
];
let processingState = {
processedItems: [],
unprocessedItems: allData.map((i) => i.id),
};
console.log("starting processing of %d items", allData.length);
console.log("> processingState:", processingState);
try {
for await (const result of processInBatches(allData, 5)) {
console.log("> batch process result:", result);
processingState = {
processedItems: [
...processingState.processedItems,
...result.batch.map((i) => i.id),
],
unprocessedItems: processingState.unprocessedItems.filter(
(i) => !result.batch.map((i) => i.id).includes(i)
),
};
}
} catch (err) {
console.error("error processing data:", err);
} finally {
console.log("done - processingState:", processingState);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment