Returning Promises from .then()
You'll learn how returning a promise from a .then() handler keeps your chain flat and elegant.
When you return a promise from inside a .then() handler, JavaScript automatically "unwraps" it for you. This means the next .then() in the chain waits for that returned promise to settle, then receives its resolved value—not the promise object itself.
Why This Matters
Without this feature, you'd end up with nested promises (a promise inside a promise), which brings back the same "rightward drift" problem you escaped from callback hell. Instead, returning a promise keeps everything linear:
fetch('/user')
.then(response => {
// Return a promise from .then()
return fetch(`/posts/${response.userId}`);
})
.then(posts => {
// This receives the posts data, NOT a promise
console.log(posts);
});
Notice how the second .then() receives posts directly, even though fetch() returns a promise. JavaScript flattened the chain for you.
The Analogy
Think of it like a relay race where each runner can either hand off a baton (a value) or a ticket to get the baton later (a promise). The next runner automatically waits to receive the actual baton before starting, regardless of which they received.
What Gets Flattened
somePromise
.then(value => {
return anotherPromise; // Returns a promise
})
.then(result => {
// 'result' is what anotherPromise resolved to
// NOT the promise itself
});
This flattening happens automatically—you don't nest .then() inside .then().
Key Takeaway: Returning a promise from .then() keeps your chain flat; the next handler automatically waits for that promise to resolve and receives its value, not the promise wrapper.