Bird
0
0

What will be logged to the console when the following Node.js code runs?

medium📝 Predict Output Q4 of 15
Node.js - Error Handling Patterns
What will be logged to the console when the following Node.js code runs?
process.on('unhandledRejection', (reason) => {
  console.error('Unhandled rejection:', reason.message);
});

Promise.reject(new Error('Connection lost'));
AUnhandled rejection: undefined
BError: Connection lost
CUnhandled rejection: Connection lost
DNo output, promise rejection is handled silently
Step-by-Step Solution
Solution:
  1. Step 1: Understand the event handler

    The handler listens for unhandled promise rejections and logs the error message.
  2. Step 2: Analyze the rejected promise

    The promise is rejected with new Error('Connection lost'), so reason.message is 'Connection lost'.
  3. Final Answer:

    Unhandled rejection: Connection lost -> Option C
  4. Quick Check:

    Correctly logs error message from rejection [OK]
Quick Trick: reason.message contains the error message [OK]
Common Mistakes:
  • Assuming the entire error object is logged instead of the message
  • Expecting no output because rejection is unhandled
  • Using reason instead of reason.message in the log

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes