type WorkerFunction = (arg: T) => Promise; export class Chunker { static Instantiate() { return new Chunker(); } async ExecuteInChunks( chunkSize: number, dataSet: T[], worker: WorkerFunction, ) { const chunks: T[][] = []; for (let i = 0; i < dataSet.length; i += chunkSize) { chunks.push(dataSet.slice(i, i + chunkSize)); } for (const chunk of chunks) { // NOTE: This is on purpose. We don't want to overwhelm // the server with requests spawned, so we rate limit to `chunksize` at a time // eslint-disable-next-line no-await-in-loop await Promise.all(chunk.map((x) => worker(x))); } } }