0
0
Node.jsframework~10 mins

Promise chaining in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Promise chaining
Create initial Promise
Resolve or Reject initial Promise
Call .then() with first handler
Return value or Promise from first handler
Call next .then() with next handler
Repeat until no more .then()
Final resolved value or error handled
This flow shows how promises are created, resolved, and how each .then() handler runs in sequence passing results along.
Execution Sample
Node.js
Promise.resolve(1)
  .then(x => x + 1)
  .then(x => Promise.resolve(x + 1))
  .then(console.log);
This code creates a promise starting with 1, then adds 1 twice, and finally logs the result.
Execution Table
StepActionInput ValueHandler ResultNext Promise StateOutput
1Create initial resolved PromiseN/APromise resolved with 1Resolved with 1N/A
2First .then() handler runs11 + 1 = 2Resolved with 2N/A
3Second .then() handler runs2Returns Promise resolved with 3Resolved with 3N/A
4Third .then() handler runs3console.log(3)Resolved with undefined3
5Chain endsN/AN/AN/AN/A
💡 No more .then() handlers, promise chain completes with final output 3
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Promise ValueN/A23undefinedundefined
Key Moments - 3 Insights
Why does the second .then() return a Promise instead of a direct value?
Because the handler returns Promise.resolve(x + 1), the chain waits for this Promise to resolve before continuing, as shown in step 3 of the execution_table.
What happens if a .then() handler returns a direct value instead of a Promise?
The value is automatically wrapped in a resolved Promise, so the next .then() receives that value immediately, like in step 2 where x + 1 returns 2 directly.
Why is the final output logged only after all .then() handlers complete?
Because each .then() waits for the previous Promise to resolve, ensuring the final console.log runs with the fully processed value, as shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Promise state after the first .then() handler runs?
AResolved with 2
BResolved with 1
CResolved with 3
DPending
💡 Hint
Check the 'Next Promise State' column at step 2 in the execution_table.
At which step does the chain handle a Promise returned inside a .then() handler?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the step where the handler returns Promise.resolve(x + 1) in the execution_table.
If the second .then() returned x + 1 directly instead of a Promise, how would the 'Next Promise State' at step 3 change?
AIt would be Rejected
BIt would be Pending
CIt would be Resolved with 3 immediately
DIt would stay Resolved with 2
💡 Hint
Refer to the variable_tracker and execution_table rows for step 3 to understand Promise resolution.
Concept Snapshot
Promise chaining lets you run async tasks in order.
Each .then() waits for the previous Promise to resolve.
Handlers can return values or Promises.
Returned Promises delay the chain until resolved.
Final .then() gets the last resolved value.
Use chaining to avoid nested callbacks.
Full Transcript
Promise chaining in Node.js means linking multiple asynchronous steps so each waits for the previous to finish. We start with a Promise, then add .then() handlers. Each handler receives the resolved value from the previous Promise. If a handler returns a direct value, it is wrapped in a resolved Promise automatically. If it returns a Promise, the chain waits for it to resolve before moving on. This way, you can run async tasks in sequence without nesting. The final .then() runs after all previous Promises resolve, receiving the final result. This example starts with Promise.resolve(1), adds 1 twice through handlers, and logs the final value 3. The execution table shows each step's input, output, and Promise state, helping beginners see how values flow through the chain.