0
0
Node.jsframework~10 mins

Promise catch for async errors in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Promise catch for async errors
Start async operation
Promise created
Async operation succeeds?
NoError thrown
Catch error
Resolve promise
Handle error
Then block runs
End
This flow shows how a Promise runs an async task, then either resolves successfully or catches errors to handle them.
Execution Sample
Node.js
function fetchData() {
  return new Promise((resolve, reject) => {
    setTimeout(() => reject('Network error'), 1000);
  });
}

fetchData()
  .then(data => console.log('Data:', data))
  .catch(err => console.log('Error:', err));
This code creates a Promise that fails after 1 second, then catches and logs the error.
Execution Table
StepActionPromise StateOutput
1fetchData() called, Promise createdPending
2setTimeout triggers after 1s, reject calledRejected
3Promise catch runs due to rejectionRejectedError: Network error
4No then block runs because Promise rejectedRejected
5Execution ends after catch handles errorRejectedError logged
💡 Promise rejected, catch block handled the error, stopping further then execution.
Variable Tracker
VariableStartAfter 1sFinal
Promise StatePendingRejectedRejected
OutputError: Network error
Key Moments - 2 Insights
Why doesn't the then block run when the Promise is rejected?
Because the Promise state changes to rejected (see step 2 and 3 in execution_table), only the catch block runs to handle the error.
What happens if we don't add a catch block?
Without catch, the rejected Promise causes an unhandled rejection error, which can crash the program or show warnings (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Promise state at step 2?
ARejected
BResolved
CPending
DSettled
💡 Hint
Check the 'Promise State' column at step 2 in execution_table.
At which step does the catch block run?
AStep 1
BStep 4
CStep 3
DStep 5
💡 Hint
Look for 'Promise catch runs' in the Action column of execution_table.
If the Promise resolved successfully instead of rejecting, what would change in the execution_table?
AStep 2 would show Rejected state
BStep 3 would show then block running instead of catch
CStep 5 would show error logged
DNo change, catch still runs
💡 Hint
Refer to the Promise State and Output columns in execution_table for success vs error.
Concept Snapshot
Promise catch for async errors:
- Create a Promise for async work
- If error occurs, reject the Promise
- Use .catch() to handle errors
- .then() runs only if Promise resolves
- Catch prevents unhandled rejections
- Always add catch for safe async code
Full Transcript
This visual trace shows how a Promise handles asynchronous errors using catch. First, the Promise is created and starts pending. After 1 second, the Promise rejects with an error. Because of rejection, the catch block runs to handle the error, logging it. The then block does not run because the Promise did not resolve successfully. This pattern helps safely manage errors in async code by catching them and preventing crashes or warnings. Always add a catch block after then to handle possible errors.