BACK
Use casesPer-item error isolation — one fail doesn't break the batch
Use cases · 62

Per-item error isolation — one fail doesn't break the batch

Pattern

We upload 4 files. One (bad.png) failed — but that must NOT cancel the other three. Each item should live and fail independently. Where you place catchError is critical.

The problem it solves

Promise.all rejects on the first error — the successful results are lost. Same logic in RxJS: catchError OUTSIDE mergeMap stops the whole batch. You must put it INSIDE each item's handling.

Operators and why they matter

  • mergeMap(file => upload(file)) — start the upload for each file in parallel.
  • catchError(...) INSIDE the inner pipeline — catches the error of that specific item only.
  • of(failedResult) — turn the error into data: {file, status: 'failed', reason}.

Gotchas

  • catchError OUTSIDE mergeMap — the batch stops on the first failed item. The most common mistake.
  • concatMap gives sequential order but everything queues — slow. Choose by UX.
  • Don't hide an item error from the UI. The user must see "file X didn't upload", or they'll think everything is fine.

What you get

The batch keeps going, and the failed item becomes part of the result (data, not a crash).

script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...