Bird
0
0

How can you convert unhandled promise rejections into exceptions that crash the Node.js process?

hard📝 Application Q9 of 15
Node.js - Error Handling Patterns
How can you convert unhandled promise rejections into exceptions that crash the Node.js process?
AUse process.on('unhandledRejection', reason => { console.log(reason); })
BUse process.on('unhandledRejection', reason => { throw reason; })
CUse Promise.catch globally to catch all rejections
DUse process.exit(1) immediately after Promise.reject
Step-by-Step Solution
Solution:
  1. Step 1: Understand converting rejection to exception

    Throwing the rejection reason inside the 'unhandledRejection' event handler causes the process to crash with an exception.
  2. Step 2: Correct event handler usage

    Using process.on('unhandledRejection', reason => { throw reason; }) achieves this behavior.
  3. Final Answer:

    Use process.on('unhandledRejection', reason => { throw reason; }) -> Option B
  4. Quick Check:

    Throw in handler = crash on unhandled rejection [OK]
Quick Trick: Throw reason inside unhandledRejection handler to crash [OK]
Common Mistakes:
  • Logging without throwing does not crash process
  • Trying to catch all rejections globally with Promise.catch
  • Calling process.exit immediately after rejection

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes