0
0
Javascriptprogramming~10 mins

Creating promises in Javascript - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating promises
Start
Create new Promise
Executor function runs
Call resolve(value)
Promise fulfilled
Call reject(error)
Promise rejected
Promise settles (fulfilled or rejected)
Then or catch handlers run
End
This flow shows how a new Promise is created, the executor function runs, and either resolve or reject is called to settle the promise, triggering then or catch handlers.
Execution Sample
Javascript
const p = new Promise((resolve, reject) => {
  setTimeout(() => resolve('Done'), 1000);
});
p.then(result => console.log(result));
Creates a promise that resolves with 'Done' after 1 second, then logs the result.
Execution Table
StepActionExecutor FunctionPromise StateOutput
1Create PromiseExecutor function startspendingnone
2setTimeout scheduledWaiting 1 secondpendingnone
3Timeout firesCalls resolve('Done')fulfillednone
4Then handler runsReceives 'Done'fulfilledLogs: Done
5EndPromise settledfulfillednone
💡 Promise settles after resolve is called, then handler runs and logs output.
Variable Tracker
VariableStartAfter 1After 2After 3Final
pPromise pendingPromise pendingPromise pendingPromise fulfilledPromise fulfilled
resultundefinedundefinedundefinedundefined'Done'
Key Moments - 3 Insights
Why does the promise start in 'pending' state and not immediately 'fulfilled'?
Because the executor function runs immediately but the asynchronous operation inside (setTimeout) delays calling resolve, so the promise stays 'pending' until resolve is called (see execution_table step 2 and 3).
What happens if reject is called instead of resolve?
The promise state changes to 'rejected' and the catch handler would run instead of then (not shown here, but similar flow to step 3 and 4).
Why does the then handler run after the promise is fulfilled?
Because then handlers wait for the promise to settle (fulfilled or rejected) before running, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the promise state at step 2?
Arejected
Bfulfilled
Cpending
Dsettled
💡 Hint
Check the 'Promise State' column at step 2 in the execution_table.
At which step does the then handler run and log the result?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for 'Then handler runs' and 'Logs: Done' in the Output column of execution_table.
If the executor called reject instead of resolve, what would change in the execution table?
APromise state would be 'rejected' at step 3
BThen handler would run at step 4
CPromise state would be 'fulfilled' at step 3
DNo change in promise state
💡 Hint
Recall that calling reject changes promise state to 'rejected' instead of 'fulfilled' (see key_moments).
Concept Snapshot
Creating Promises in JavaScript:
- Use new Promise((resolve, reject) => {...})
- Executor runs immediately
- Call resolve(value) to fulfill
- Call reject(error) to reject
- Then/catch handle results asynchronously
- Promise starts as 'pending' and settles later
Full Transcript
This visual trace shows how creating a promise works in JavaScript. First, a new Promise is created and the executor function runs immediately. Inside, asynchronous work like setTimeout is scheduled. The promise starts in a 'pending' state. When resolve is called after 1 second, the promise state changes to 'fulfilled'. Then, the then handler runs and logs the result 'Done'. This flow helps understand how promises manage asynchronous operations by settling later and triggering handlers.