Course contentsShow
← JavaScript
Lesson 1322 of 2,72329. PromisesFree lesson

Catch Placement: End vs Middle of Chain

How a single catch at the end differs from catches placed between then calls for granular recovery.

Catch Placement: End vs Middle of Chain

You'll learn how where you place .catch() in a promise chain dramatically changes error handling behavior and recovery options.

The Core Idea

A .catch() at the end of a chain acts like a single safety net, catching any error from any step above it. A .catch() placed in the middle catches errors only from steps before it, allows you to recover, and lets the chain continue afterward.

Think of it like a safety net in a circus: one net at the bottom catches anyone who falls from any height. But if you place nets at different levels, a performer can fall, land on a middle net, recover, and keep performing.

End-of-Chain Catch

fetchUser()
  .then(user => fetchOrders(user.id))
  .then(orders => processOrders(orders))
  .then(result => console.log('Success:', result))
  .catch(error => console.error('Failed:', error));

Here, .catch() catches errors from fetchUser(), fetchOrders(), or processOrders(). The chain stops when any error occurs.

Middle-of-Chain Catch for Recovery

fetchUser()
  .then(user => fetchOrders(user.id))
  .catch(error => {
    console.warn('Orders failed, using empty array');
    return []; // Recovery!
  })
  .then(orders => processOrders(orders)) // Still runs
  .then(result => console.log('Success:', result));

The middle .catch() intercepts the fetchOrders() error, returns a default value, and the chain continues. This enables granular error recovery.

You can have multiple catches for different recovery strategies at different points.

Key Takeaway: Place .catch() at the end for centralized error handling that stops the chain, or in the middle to recover from specific failures and allow the promise chain to continue with fallback values.