Bird
0
0

What will happen if an error is passed to the callback in this code?

medium📝 component behavior Q5 of 15
Node.js - Error Handling Patterns
What will happen if an error is passed to the callback in this code?
function getUser(callback) {
  callback(new Error('User not found'), null);
}
getUser((err, user) => {
  if (err) {
    console.error(err.message);
  } else {
    console.log(user);
  }
});
ALogs: User not found
BLogs: null
CLogs: undefined
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Identify the error passed to callback

    The callback receives an Error object with message 'User not found'.
  2. Step 2: Check callback error handling

    The if block logs the error message using console.error.
  3. Final Answer:

    Logs: User not found -> Option A
  4. Quick Check:

    Error passed triggers error handling block [OK]
Quick Trick: Error argument triggers error handling code [OK]
Common Mistakes:
  • Logging user when error exists
  • Expecting null or undefined output
  • Ignoring error argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes