JSFiddle - React, Tailwind, and code Playground
by Nic Fontaine
TypeScript
const batches = Array.from({ length: 100 }).map((_, i) => i);
const MAX_GROUP_SIZE = 10;
const groups = batches.reduce(
(acc: number[][], curr) => {
const lastBatch = acc[acc.length - 1];
if (lastBatch.length >= MAX_GROUP_SIZE) {
acc.push([curr]);
} else {
lastBatch.push(curr);
}
return acc;
},
[[]]
);
Promise.all(groups.map(async (group, groupIdx) => {
console.log('---------------------');
console.log(`Group ${groupIdx} ...`);
return await Promise.all(group.map((batch) => {
console.log(`Batch ${batch} ...`);
return batch;
}));
}));