diginext-utils
Version:
README.md
14 lines (13 loc) • 543 B
JavaScript
export default async function batchProcessor(list, callback, batchSize = 10) {
let res = [];
// Function to process a single batch
async function processBatch(batch) {
return await Promise.all(batch.map((item) => callback(item)));
}
// Divide the list into batches
for (let i = 0; i < list.length; i += batchSize) {
const batch = list.slice(i, i + batchSize);
res = [...res, ...(await processBatch(batch))]; // Process each batch and wait for it to complete
}
return res;
}