Returning Values from .then()
What you'll learn: How any value you return from a .then() handler automatically becomes a resolved promise for the next link in the chain.
The Core Behavior
When you return a value from a .then() callback, JavaScript doesn't just pass that raw value to the next .then() — it wraps it in a resolved promise first. This happens automatically, behind the scenes.
Promise.resolve(5)
.then(value => {
console.log(value); // 5
return value * 2; // Returns 10
})
.then(doubled => {
console.log(doubled); // 10 — JavaScript wrapped 10 in a resolved promise
});
Even though you wrote return value * 2, the next .then() receives that value as if you'd written return Promise.resolve(value * 2).
Why This Matters
This automatic wrapping is what makes promise chaining seamless. You can return simple values, and the chain continues smoothly. You don't need to manually create promises for every transformation.
fetch('/user')
.then(response => response.json()) // Returns a promise
.then(user => user.name) // Returns a string
.then(name => name.toUpperCase()) // Returns a string
.then(upperName => {
console.log(upperName); // All values flow through as promises
});
The Analogy
Think of .then() as a conveyor belt at a factory. Whatever you place on the belt (your return value) gets automatically packaged in a standard box (a promise) before moving to the next station. You don't have to box it yourself — the system handles it.
Key Takeaway: Returning a plain value from .then() automatically wraps it in a resolved promise, allowing smooth chaining without manual promise creation.