Bird
0
0

What will be logged to the console when running this code?

medium📝 component behavior Q4 of 15
Node.js - Error Handling Patterns
What will be logged to the console when running this code?
async function fetchData() {
  try {
    const data = await Promise.reject('Network error');
    console.log('Data:', data);
  } catch (err) {
    console.log('Caught:', err);
  }
}
fetchData();
ACaught: Network error
BUncaught (in promise) Network error
CData: Network error
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand Promise.reject behavior

    Promise.reject immediately rejects with the given error.
  2. Step 2: Analyze try/catch with await

    Await throws the rejection error, caught by catch block logging 'Caught: Network error'.
  3. Final Answer:

    Caught: Network error -> Option A
  4. Quick Check:

    Rejected promise caught = C [OK]
Quick Trick: Rejected promises throw errors caught by try/catch [OK]
Common Mistakes:
  • Expecting console.log inside try to run
  • Missing catch block causes unhandled rejection
  • Confusing reject with resolve

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes