Work in progress - please don't mind the momentary mess
Helper functions to create and apply chunks. It's a wrapper around lodash chunk that provides additional logging and a way to process the chunks.
Using npm
npm i koan-chunked
Add to the top of the file
import * as chunked from "koan-chunked"
/*
* @param ts {array}
* @param chunkSize {number}
*/
const original: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const groupedByTwo = chunked.from(original, 2);
/* groupedByTwo = {
"total": 10,
"chunks": [
[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10]
]
}
*/
type Chunked<T> = { total: number; chunks: T[][] };
/*
* @param chunked {Chunked<T>}
* @param applyChunk {function that returns a Promise}
*/
await chunked.apply(groupedByTwo, (chunk) =>
Promise.all(chunk.map((item) => console.log(item)),
));
// This will console.log each item in groupedByTwo