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

Chaining vs Nesting

Comparing flat chains with nested .then() calls and why flat is preferred.

Chaining vs Nesting

You'll learn: Why flat promise chains are clearer and more maintainable than nested .then() calls.


The Two Styles

When working with promises, you have two choices for handling sequential operations:

Flat chaining (recommended):

fetchUser()
  .then(user => fetchOrders(user.id))
  .then(orders => processOrders(orders))
  .then(result => console.log(result))
  .catch(err => console.error(err));

Nesting (callback-hell-style):

fetchUser()
  .then(user => {
    fetchOrders(user.id)
      .then(orders => {
        processOrders(orders)
          .then(result => {
            console.log(result);
          })
          .catch(err => console.error(err));
      })
      .catch(err => console.error(err));
  })
  .catch(err => console.error(err));

Why Flat Is Better

Readability: Flat chains read like a to-do list — each step naturally follows the previous. Nested promises create the same "rightward drift" that callback hell caused.

Error handling: With flat chaining, a single .catch() at the end handles errors from any step. With nesting, you often duplicate error handlers or miss errors from inner promises.

Scope simplicity: Flat chains avoid deeply nested scopes. Each .then() receives exactly what it needs from the previous step, nothing more.

Analogy: Think of assembly line workers passing items forward (flat) versus workers nested inside boxes inside boxes (nested). The flat line is obviously easier to follow and fix.

When Nesting Happens

Nesting often occurs accidentally when developers forget to return the promise from .then(), or when they're still thinking in callback patterns.

Key Takeaway: Always prefer flat promise chains over nested ones — they're easier to read, debug, and maintain, with simpler error handling.