Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/process-in-chunks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,22 @@ describe("processInChunks", () => {

describe("processInChunksByChunk", () => {
describe("core behavior", () => {
it("passes correct chunk index to callback", async () => {
const items = [1, 2, 3, 4, 5, 6];
const receivedIndices: number[] = [];

await processInChunksByChunk(
items,
async (chunk, index) => {
receivedIndices.push(index);
return chunk.length;
},
{ chunkSize: 2 },
);

expect(receivedIndices).toEqual([0, 1, 2]);
});

it("passes entire chunk array to callback", async () => {
const items = [1, 2, 3, 4, 5, 6];
const receivedChunks: number[][] = [];
Expand Down
6 changes: 1 addition & 5 deletions src/process-in-chunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,11 @@ export async function processInChunksByChunk<T, R>(
const errorMessagesSet = new Set<string>();
const results: (R | undefined)[] = [];

let overallIndex = 0;

for (const [index, items] of chunks.entries()) {
logIfVerbose(`Processing chunk ${index + 1}/${chunks.length}`);

try {
const processPromise = processFn(items, overallIndex);
const processPromise = processFn(items, index);

/** Run throttle wait in parallel with processing if throttling is enabled */
const result = await (throttleSeconds > 0
Expand All @@ -154,14 +152,12 @@ export async function processInChunksByChunk<T, R>(
: processPromise);

results.push(result);
overallIndex += items.length;
} catch (err) {
if (!noThrow) {
throw err; // Rethrow original error immediately
}
errorMessagesSet.add(getErrorMessage(err));
results.push(undefined);
overallIndex += items.length;
}
}

Expand Down