0
0
Node.jsframework~20 mins

Unhandled rejection handling in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unhandled Rejection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What happens when a Promise rejection is not handled?

Consider a Node.js program where a Promise is rejected but no catch or rejection handler is attached. What is the typical behavior?

ANode.js emits an <code>unhandledRejection</code> event but does not terminate the process by default.
BNode.js immediately crashes with a fatal error.
CThe program silently ignores the rejection and continues running.
DThe rejection is automatically retried until it succeeds.
Attempts:
2 left
💡 Hint

Think about how Node.js informs you about unhandled promise rejections before deciding what to do.

component_behavior
intermediate
2:00remaining
What is the output of this unhandled rejection handler code?

Given the following Node.js code, what will be printed to the console?

Node.js
process.on('unhandledRejection', (reason, promise) => {
  console.log('Unhandled Rejection at:', promise, 'reason:', reason.message);
});

Promise.reject(new Error('Fail!'));
ASyntaxError due to incorrect event handler syntax.
BUnhandled Rejection at: undefined reason: Fail!
CNo output, the rejection is handled silently.
DUnhandled Rejection at: [object Promise] reason: Fail!
Attempts:
2 left
💡 Hint

Look at the parameters passed to the unhandledRejection event handler.

🔧 Debug
advanced
2:00remaining
Why does this unhandled rejection handler not catch the error?

Examine this code snippet. Why does the unhandledRejection event not trigger?

Node.js
process.on('unhandledRejection', (reason) => {
  console.log('Caught:', reason.message);
});

const p = Promise.reject(new Error('Oops!'));
p.catch(() => {});
AThe event handler is missing the second parameter, so it fails silently.
BBecause the Promise has a catch handler, the rejection is considered handled and no event is emitted.
CThe event name is misspelled, so it never triggers.
DPromises rejected inside event handlers do not emit unhandledRejection.
Attempts:
2 left
💡 Hint

Think about when Node.js emits the unhandledRejection event.

📝 Syntax
advanced
2:00remaining
Which option correctly attaches a global unhandled rejection handler?

Choose the code snippet that correctly listens for unhandled Promise rejections in Node.js.

Aprocess.addListener('unhandledRejection', reason => console.log(reason));
Bprocess.onUnhandledRejection((reason) => console.log(reason));
Cprocess.on('unhandledRejection', (reason, promise) => { console.log(reason); });
Dprocess.handle('unhandledRejection', (reason) => console.log(reason));
Attempts:
2 left
💡 Hint

Recall the correct Node.js API for event listening on process.

lifecycle
expert
2:00remaining
What is the effect of calling 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?

AThe error message is logged, and the Node.js process exits immediately with code 1.
BThe error message is logged, but the process continues running normally.
CThe unhandledRejection event is ignored because <code>process.exit()</code> is called.
DThe process crashes with an unhandled exception error before logging.
Attempts:
2 left
💡 Hint

Think about what process.exit() does inside event handlers.