Handling Partial Failures
Processing lists of items where individual failures shouldn't stop the entire process.
When processing a batch of items (like sending multiple emails or updating many records), a single failure often shouldn't crash the entire job. You need to know which items succeeded and which failed to generate a report.
Combine batch processing with partitioning to separate successes from failures into two distinct lists.
bulk-email.ts
async function processJob(items: string[]) {
const results = await catchErrorAll(
items.map(id => catchError(sendEmail(id)))
);
const { ok: sent, err: failed } = partition(results);
console.log(`Successfully sent: ${sent.length}`);
console.log(`Failed: ${failed.length}`);
return { sent, failed };
}