Consider a Node.js program where a Promise is rejected but no catch or rejection handler is attached. What is the typical behavior?
Think about how Node.js informs you about unhandled promise rejections before deciding what to do.
Node.js emits an unhandledRejection event when a Promise is rejected without a handler. By default, it logs a warning but does not crash the process immediately.
Given the following Node.js code, what will be printed to the console?
process.on('unhandledRejection', (reason, promise) => { console.log('Unhandled Rejection at:', promise, 'reason:', reason.message); }); Promise.reject(new Error('Fail!'));
Look at the parameters passed to the unhandledRejection event handler.
The event handler receives the rejected Promise and the reason (Error object). Logging them shows the Promise object and the error message.
Examine this code snippet. Why does the unhandledRejection event not trigger?
process.on('unhandledRejection', (reason) => { console.log('Caught:', reason.message); }); const p = Promise.reject(new Error('Oops!')); p.catch(() => {});
Think about when Node.js emits the unhandledRejection event.
If a Promise rejection is handled by a catch or then handler, Node.js does not emit the unhandledRejection event.
Choose the code snippet that correctly listens for unhandled Promise rejections in Node.js.
Recall the correct Node.js API for event listening on process.
The correct method is process.on(eventName, callback). Options C and D use invalid method names.
process.exit() inside an unhandled rejection handler?Consider this code snippet:
process.on('unhandledRejection', (reason) => {
console.error('Error:', reason.message);
process.exit(1);
});
Promise.reject(new Error('Critical failure'));What happens when this code runs?
Think about what process.exit() does inside event handlers.
Calling process.exit(1) stops the Node.js process immediately after logging the error.