Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is an unhandled rejection in Node.js?
An unhandled rejection happens when a Promise is rejected but no code catches or handles that rejection. It can cause the program to crash or behave unexpectedly.
Click to reveal answer
beginner
How do you listen for unhandled promise rejections globally in Node.js?
You use the 'unhandledRejection' event on the process object, like this: process.on('unhandledRejection', (reason, promise) => { /* handle it */ });
Click to reveal answer
beginner
Why is it important to handle unhandled rejections?
Because unhandled rejections can cause your app to crash or leave errors unnoticed, handling them helps keep your app stable and lets you log or fix errors properly.
Click to reveal answer
intermediate
What is a common way to handle unhandled rejections in production?
A common way is to log the error details and then gracefully shut down the app or restart it to avoid running in a bad state.
Click to reveal answer
beginner
Show a simple example of handling unhandled rejections in Node.js.
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
// Optional: process.exit(1) to stop the app
});
Click to reveal answer
What event does Node.js emit when a promise rejection is not handled?
AunhandledRejection
Berror
CrejectionHandled
DpromiseRejected
✗ Incorrect
Node.js emits the 'unhandledRejection' event on the process object when a promise rejection is not handled.
What happens if you do NOT handle unhandled promise rejections in Node.js?
ANode.js automatically retries the promise
BNothing, the rejection is ignored safely
CThe app may crash or behave unpredictably
DThe promise resolves successfully
✗ Incorrect
Unhandled rejections can cause the app to crash or behave unpredictably if not handled.
Which object do you use to listen for unhandled promise rejections?
Aglobal
BPromise
Cconsole
Dprocess
✗ Incorrect
The 'process' object emits the 'unhandledRejection' event.
What parameters does the 'unhandledRejection' event handler receive?
Aerror, stack
Breason, promise
Cevent, data
Dmessage, code
✗ Incorrect
The handler receives the rejection reason (error) and the promise that was rejected.
What is a good practice after catching an unhandled rejection in production?
ALog the error and gracefully shut down or restart the app
BIgnore it and continue running
CThrow a new error immediately
DAutomatically resolve the promise
✗ Incorrect
Logging and graceful shutdown help maintain app stability after errors.
Explain what an unhandled rejection is and why it matters in Node.js applications.
Think about what happens when a promise fails but no one catches the error.
You got /3 concepts.
Describe how to set up a global handler for unhandled promise rejections in Node.js and what you might do inside that handler.
Consider the event name and what actions help keep the app safe.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using process.on('unhandledRejection') in a Node.js application?
easy
A. To catch errors from promises that were not handled anywhere else
B. To handle synchronous errors in try-catch blocks
C. To restart the Node.js server automatically
D. To log all successful promise resolutions
Solution
Step 1: Understand what unhandled rejections are
Unhandled rejections happen when a promise fails but no .catch() or try-catch handles the error.
Step 2: Role of process.on('unhandledRejection')
This event listener catches those unhandled promise errors so you can log or clean up before the app crashes.
Final Answer:
To catch errors from promises that were not handled anywhere else -> Option A
But the console shows Caught: Oops! instead of an error message. What is the issue?
medium
A. The handler function must be async to catch rejections
B. The event name should be 'unhandledRejections' (plural)
C. You must use try-catch instead of process.on for promises
D. The rejection reason is a string, not an Error object, so error.message is undefined
Solution
Step 1: Check the rejection reason type
The promise rejects with a string 'Oops!', not an Error object.
Step 2: Understand how the handler logs the error
The handler logs the whole error variable, which is the string 'Oops!', so it prints exactly that.
Final Answer:
The rejection reason is a string, not an Error object, so error.message is undefined -> Option D
Quick Check:
Rejection reason type affects error.message presence [OK]
Hint: Rejection reason can be any type; strings have no .message [OK]
Common Mistakes:
Using wrong event name 'unhandledRejections'
Thinking async handler is required
Believing try-catch catches unhandled rejections
5. You want to log unhandled promise rejections and then gracefully shut down your Node.js server. Which code snippet correctly implements this behavior?
hard
A. process.on('unhandledRejection', (reason) => {
console.error('Unhandled rejection:', reason);
process.exit(1);
});
B. process.on('unhandledRejection', (reason) => {
console.log('Handled rejection:', reason.message);
});
Graceful shutdown means closing server connections before exiting the process.
Step 2: Analyze each option's shutdown approach
process.on('unhandledRejection', (reason) => {
console.error('Unhandled rejection:', reason);
server.close(() => process.exit(1));
}); logs the error, then calls server.close() with a callback to exit after closing. This is correct.
Step 3: Identify why others are incorrect
process.on('unhandledRejection', (reason) => {
console.error('Unhandled rejection:', reason);
process.exit(1);
}); exits immediately without closing server; C only logs without shutdown; D awaits server.close but does not exit process.