0
0
Javascriptprogramming~10 mins

Promise error handling in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Promise error handling
Create Promise
Promise runs async task
Task succeeds?
NoReject with error
Catch error handler
Resolve with value
Handle error
Then handler runs
End
This flow shows how a Promise runs an async task, then either resolves successfully or rejects with an error, which can be caught and handled.
Execution Sample
Javascript
const p = new Promise((resolve, reject) => {
  reject('Error happened');
});

p.then(value => console.log('Success:', value))
 .catch(err => console.log('Caught:', err));
This code creates a Promise that immediately rejects with an error, then catches and logs that error.
Execution Table
StepActionPromise StateHandler CalledOutput
1Create Promisependingnone
2Promise executor calls reject('Error happened')rejectednone
3Then handler skipped (promise rejected)rejectednone
4Catch handler called with errorrejectedcatchCaught: Error happened
5End of promise chainsettlednone
💡 Promise is rejected, catch handler handles the error, chain ends.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
p (Promise)pendingrejectedrejectedsettled
errorundefined'Error happened''Error happened''Error happened'
Key Moments - 2 Insights
Why does the then handler not run when the promise is rejected?
Because in the execution_table at step 3, the promise state is rejected, so the then handler is skipped and only the catch handler runs.
How does the catch handler receive the error?
At step 4 in the execution_table, the catch handler is called with the rejection reason 'Error happened' passed as the error argument.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the promise state after step 2?
Arejected
Bresolved
Cpending
Dsettled
💡 Hint
Check the 'Promise State' column at step 2 in the execution_table.
At which step does the catch handler run?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Handler Called' and 'Output' columns in the execution_table.
If the promise resolved instead of rejected, which handler would run?
Acatch handler
Bthen handler
Cboth handlers
Dno handler
💡 Hint
Refer to the concept_flow where success leads to then handler running.
Concept Snapshot
Promise error handling:
- Create a Promise with resolve and reject
- If async task fails, call reject(error)
- Use .catch() to handle errors
- .then() runs only on success
- Errors skip then and go to catch
Full Transcript
This visual trace shows how a JavaScript Promise handles errors. First, a Promise is created and starts pending. Then the executor calls reject with an error, changing the state to rejected. Because the Promise is rejected, the then handler is skipped and does not run. Instead, the catch handler runs and receives the error, logging it. Finally, the Promise chain ends. This shows that catch is used to handle errors from rejected Promises, while then only runs on success.