JavaScript · Promises · Junior · Concept
How does promise chaining work, and what happens if a `.then` handler returns a value vs a promise?
Short Interview Answer
Each `.then` returns a new promise; returned values fulfill it, thrown errors reject it, and returned promises adopt that promise's state.
Detailed Explanation
Chaining avoids nesting by passing results along. If you return a plain value, the next `.then` receives it. If you return a thenable/promise, the chain waits for it (assimilation). If you omit a return, the next handler gets `undefined`. Throwing or returning `Promise.reject` rejects the downstream chain until a `.catch`. Always return/throw intentionally inside handlers to keep control flow clear.
Example
Promise.resolve(1)
.then((n) => n + 1)
.then((n) => Promise.resolve(n * 2))
.then(console.log); // 4Interview Tip
Warn about missing `return` inside `.then` — common bug.
Common Mistake
Nesting `.then` inside `.then` unnecessarily (recreating callback hell).