Bird
0
0

Given this code, what will be printed?

medium📝 Predict Output Q5 of 15
Node.js - Error Handling Patterns
Given this code, what will be printed?
process.on('unhandledRejection', (reason, promise) => {
  console.log('Promise rejected:', reason);
});

Promise.reject('Error occurred');
APromise rejected: [object Object]
BNo output
CPromise rejected: Error occurred
DPromise rejected: undefined
Step-by-Step Solution
Solution:
  1. Step 1: Check event listener parameters

    The listener logs the reason parameter directly, which is the rejection reason.
  2. Step 2: Analyze rejection value

    The promise is rejected with the string 'Error occurred', so the reason is that string.
  3. Final Answer:

    Promise rejected: Error occurred -> Option C
  4. Quick Check:

    Rejection reason string logs as is [OK]
Quick Trick: Rejection reason logs directly in unhandledRejection handler [OK]
Common Mistakes:
  • Assuming reason is always an Error object
  • Expecting no output for string rejection
  • Confusing reason with promise object

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes