0
0
Node.jsframework~5 mins

Unhandled rejection handling in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
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
Which object do you use to listen for unhandled promise rejections?
Aglobal
BPromise
Cconsole
Dprocess
What parameters does the 'unhandledRejection' event handler receive?
Aerror, stack
Breason, promise
Cevent, data
Dmessage, code
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
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.