import { errorToString } from "../utils/errors.ts"; export type ItemResult = { success: boolean; error?: string; data?: T; }; export type BatchSummary = { total: number; successes: number; failures: number; firstError?: string; results: ItemResult[]; }; export async function runSequentialBatch( items: I[], run: (item: I) => Promise>, ): Promise> { const results: ItemResult[] = []; let successes = 0; let failures = 0; let firstError: string | undefined; for (const item of items) { let result: ItemResult; try { result = await run(item); } catch (e) { result = { success: false, error: errorToString(e) }; } results.push(result); if (result.success) { successes++; } else { failures++; if (firstError === undefined && result.error) { firstError = result.error; } } } return { total: items.length, successes, failures, firstError, results, }; }