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

What Promise Chaining Is

Understanding how .then() returns a new promise, allowing sequential async operations.

What Promise Chaining Is

You'll learn how returning values from .then() creates a chain of promises that execute in sequence.

The Core Idea

Promise chaining is the technique of calling .then() on a promise, which itself returns a new promise, allowing you to attach another .then() to that result. This creates a sequential flow of asynchronous operations where each step waits for the previous one to complete.

Every .then() automatically wraps its return value in a new promise. If you return a plain value, it becomes a resolved promise with that value. If you return a promise, the chain waits for that promise to settle before continuing.

fetchUser(123)
  .then(user => {
    console.log('Got user:', user.name);
    return fetchPosts(user.id);  // returns a promise
  })
  .then(posts => {
    console.log('Got posts:', posts.length);
    return posts[0];  // returns a plain value
  })
  .then(firstPost => {
    console.log('First post:', firstPost.title);
  });

Each .then() in this chain executes only after the previous promise resolves. The value you return becomes the input to the next .then().

The Analogy

Think of promise chaining like a relay race. Each runner (.then() handler) waits for the baton (the resolved value) before running their leg. When they finish, they pass a new baton to the next runner. The race only moves forward one runner at a time.

Why This Matters

Chaining solves the "rightward drift" of callback hell by keeping async operations flat and readable. Instead of nesting callbacks inside callbacks, you stack .then() calls vertically, making the sequence obvious.

Key Takeaway: Every .then() returns a new promise, and that promise resolves with whatever you return from the handler—enabling clean, sequential async operations without nesting.