0
0
Node.jsframework~10 mins

Error handling in async/await in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Error handling in async/await
Start async function
Try block: await async call
Async call success?
NoCatch block: handle error
Error handled/logged
Continue normal flow
End
This flow shows how async/await uses try/catch to handle errors from asynchronous calls, allowing normal flow if no error occurs or error handling if one happens.
Execution Sample
Node.js
async function fetchData() {
  try {
    const data = await fetch('https://api.example.com/data');
    return await data.json();
  } catch (error) {
    console.error('Fetch error:', error);
  }
}
This code tries to fetch data asynchronously and catches any error that happens during the fetch or JSON parsing.
Execution Table
StepActionEvaluationResult
1Call fetchData()Enter async functionStart try block
2await fetch('https://api.example.com/data')Fetch request sentPromise pending
3Fetch resolves successfullyResponse receivedResponse object stored in data
4await data.json()Parse JSONParsed JSON returned
5Return parsed JSONFunction resolvesData returned to caller
6Function endsNo error thrownNormal completion
💡 Function ends normally because no error occurred during fetch or JSON parsing
Variable Tracker
VariableStartAfter Step 3After Step 4Final
dataundefinedResponse objectResponse objectResponse object
Key Moments - 2 Insights
Why do we use try/catch with async/await?
Because await pauses the function until the promise settles, errors thrown inside await calls must be caught with try/catch to avoid unhandled rejections, as shown in steps 2-5 of the execution_table.
What happens if fetch fails?
If fetch fails, the promise rejects and control jumps to the catch block (not shown in this successful run), where the error can be handled or logged, preventing the function from crashing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'data' after step 3?
Aundefined
BParsed JSON object
CResponse object
DError object
💡 Hint
Check variable_tracker column 'After Step 3' for 'data'
At which step does the function return the parsed JSON data?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at execution_table row where 'Return parsed JSON' happens
If fetch fails, which part of the flow handles the error?
ACatch block handles the error
BTry block continues normally
CFunction returns undefined immediately
DError is ignored
💡 Hint
Refer to concept_flow where 'No' branch from async call success leads to catch block
Concept Snapshot
async/await error handling:
- Use try { await asyncCall() } catch (error) { handle error }
- Await pauses until promise settles
- Errors thrown inside await go to catch
- Prevents unhandled promise rejections
- Keeps async code readable and safe
Full Transcript
This visual execution shows how error handling works with async/await in Node.js. The async function starts and enters a try block where it awaits a fetch call. If the fetch succeeds, it proceeds to parse JSON and returns the data. If any error occurs during fetch or parsing, control jumps to the catch block where the error is handled or logged. Variables like 'data' change from undefined to response object to parsed JSON. This pattern ensures asynchronous errors are caught cleanly, avoiding crashes or unhandled rejections.