Passing value into next Promises argument
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In a promise chain, the value for the next .then(...) callback comes from what the previous handler returns. You do not manually push a value into the next promise as a separate step. You either return a plain value, return another promise, or throw an error, and the chain updates itself accordingly.
The Core Rule Of .then()
A promise callback receives the previous fulfillment value. Its return value becomes the fulfillment value of the next link in the chain.
Output:
The second .then(...) gets 10 because the first handler returned value * 2.
Returning A Plain Value Versus A Promise
If you return a plain value, the next promise resolves with that value.
If you return a promise, the next step waits for that promise to resolve.
This is why promise chains flatten automatically. You do not get a promise of a promise in normal .then(...) usage.
If You Need To Pass More Than One Value
A .then(...) callback only passes one fulfillment value forward, but that value can be an object or array.
If you need multiple related values later in the chain, package them intentionally instead of relying on outer mutable variables.
A Common Mistake: Calling Instead Of Wrapping
People often write:
That is wrong because console.log("value") runs immediately, and its return value is passed to .then(...) instead of a callback function.
Correct forms are:
or, if you only need the same function reference:
The same rule applies when you want to pass an extra value into the next handler. Wrap it in a function and return what the next step should receive.
Passing A Custom Value To The Next Step
Suppose the first async operation returns data you do not want to forward directly. Return the transformed value you actually want the next handler to receive.
If you want both the original user and a derived value later, return both in one object.
Error Flow Works The Same Way
If a handler throws, the chain becomes rejected and control moves to the nearest rejection handler.
Understanding this is important because success values are passed by return, while errors are passed by throw or by returning a rejected promise.
The Async/Await Equivalent
If promise chaining feels noisy, async and await express the same flow more directly:
This does not change the underlying rule. The next step still gets whatever value you produce from the previous one. await just makes the control flow easier to read.
Common Pitfalls
- Forgetting to
returna value from a.then(...)callback and accidentally passingundefinedforward. - Calling a function immediately instead of passing a callback function into
.then(...). - Using outer mutable variables when returning an object or array would be clearer.
- Returning a promise but also nesting another
.then(...)unnecessarily. - Mixing rejection handling between the second argument of
.then(...)and.catch(...)in a confusing way.
Summary
- The next
.then(...)receives whatever the previous handler returns. - Return a plain value to pass a value forward.
- Return a promise to make the chain wait for another async result.
- If you need multiple values later, return an object or array.
- If you forget to return, the next step receives
undefined.
Related reading
- Pattern for updating slave SQL Server 2008 databases from a master whilst minimising disruption
- Patterns for Multithreaded Network Server in C
- Patterns/Principles for thread-safe queues and master/worker program in Java
- pdb cannot break in another thread?
- Perform actions as promises get fulfilled using Promise.all
- Performance of nodejs async hooks
- Perform UI Changes on main thread using dispatch_async or performSelectorOnMainThread?
- Performance Apache HttpAsyncClient vs multi-threaded URLConnection
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.