0
0
Node.jsframework~10 mins

Unhandled rejection handling in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Unhandled rejection handling
Promise created
Promise rejected?
NoPromise resolved, normal flow
Yes
Is rejection handled?
YesHandle rejection with .catch() or try/catch
No
Node.js emits 'unhandledRejection' event
Optional process exit or logging
Program continues or stops
This flow shows how Node.js handles promises that reject without a catch handler, emitting an event if unhandled.
Execution Sample
Node.js
const p = Promise.reject(new Error('fail'));
// No catch handler

process.on('unhandledRejection', (reason) => {
  console.log('Unhandled rejection:', reason.message);
});
This code creates a rejected promise without a catch, triggering the unhandledRejection event.
Execution Table
StepActionPromise StateHandler Present?Event EmittedOutput
1Create rejected promiseRejectedNoNo
2No catch handler attachedRejectedNoNo
3Event loop checks for unhandled rejectionsRejectedNoYesunhandledRejection event emitted
4Event listener logs rejection reasonRejectedNoYesUnhandled rejection: fail
5Program continues or exits based on policyRejectedNoYes
💡 Unhandled rejection detected with no handler, event emitted and logged.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
pundefinedRejected Promise(Error: fail)Rejected Promise(Error: fail)Rejected Promise(Error: fail)Rejected Promise(Error: fail)
handlerAttachedfalsefalsefalsefalsefalse
eventEmittedfalsefalsefalsetruetrue
Key Moments - 3 Insights
Why does Node.js emit an 'unhandledRejection' event?
Because the promise was rejected but no catch handler was attached, as shown in execution_table step 3.
What happens if we add a .catch() handler after the rejection?
The rejection is handled, so the 'unhandledRejection' event is not emitted. This is because handlerAttached would be true, preventing event emission.
Does the program stop immediately after an unhandled rejection?
Not necessarily. Node.js emits the event and logs it, but the program continues unless explicitly exited, as shown in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the 'unhandledRejection' event emitted?
AStep 1
BStep 3
CStep 5
DStep 2
💡 Hint
Check the 'Event Emitted' column in the execution_table rows.
According to variable_tracker, what is the value of 'handlerAttached' after step 2?
Atrue
Bundefined
Cfalse
Dnull
💡 Hint
Look at the 'handlerAttached' row and the 'After Step 2' column.
If a .catch() handler is added immediately after promise creation, how would the 'eventEmitted' variable change?
AIt would become false
BIt would remain true
CIt would become undefined
DIt would become null
💡 Hint
Refer to the explanation in key_moments about handler presence preventing event emission.
Concept Snapshot
Unhandled rejection handling in Node.js:
- When a Promise rejects without a catch, Node.js emits 'unhandledRejection'.
- Attach .catch() or use try/catch with async/await to handle rejections.
- Listen to 'unhandledRejection' on process to log or handle globally.
- Program continues unless explicitly exited.
- Helps avoid silent failures in async code.
Full Transcript
In Node.js, when a Promise is rejected but no catch handler is attached, the runtime emits an 'unhandledRejection' event. This event allows developers to detect and log errors that would otherwise be missed. The example code creates a rejected Promise without a catch handler, triggering this event. The execution table shows the promise state and event emission steps. Variables track the promise state, handler presence, and event emission. Key moments clarify why the event is emitted, what happens if a handler is added, and that the program does not stop immediately. The visual quiz tests understanding of when the event fires and variable states. This helps beginners see how unhandled rejections flow through Node.js and how to handle them properly.