The Importance of Returning
You'll learn why forgetting to return in .then() handlers breaks your promise chain and causes data to vanish.
Why Return Matters in Promise Chains
When you write a .then() handler, you're creating a link in a chain. The value you return becomes the resolved value for the next .then() in the chain. If you forget to return, JavaScript implicitly returns undefined, and that's what flows forward—not the value you intended.
Think of it like a relay race: each runner must hand off the baton to the next runner. If a runner just drops the baton instead of passing it, the next runner gets nothing.
The Missing Return Problem
fetch('/api/user')
.then(response => {
response.json(); // ❌ Forgot to return!
})
.then(data => {
console.log(data); // undefined — the data is lost!
});
Without return, the parsed JSON never reaches the next .then(). The promise resolves with undefined.
The Fixed Version
fetch('/api/user')
.then(response => {
return response.json(); // ✅ Returned!
})
.then(data => {
console.log(data); // Now data contains the parsed JSON
});
By returning response.json() (which itself returns a promise), you ensure the chain waits for that promise to resolve and passes its value forward.
Applies to Both Values and Promises
You must return whether you're returning:
- A plain value:
return value * 2; - A promise:
return fetch('/next-endpoint');
Without the return keyword, the chain loses track of your work.
Key Takeaway: Always return from .then() handlers if you want the value to flow to the next link in the chain—forgetting to return is the most common promise chaining mistake.