Bird
0
0

Consider the following code snippet:

medium📝 component behavior Q13 of 15
Node.js - Error Handling Patterns
Consider the following code snippet:
function readFile(callback) {
  setTimeout(() => {
    callback(null, 'file content');
  }, 100);
}

readFile((err, data) => {
  if (err) {
    console.log('Error:', err);
  } else {
    console.log('Data:', data);
  }
});

What will be printed to the console?
AError: null
BData: file content
CError: file content
DNothing will be printed
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the callback invocation

    The callback is called with null as error and 'file content' as data after 100ms.
  2. Step 2: Check the callback logic

    Since err is null (no error), the else branch runs and logs 'Data: file content'.
  3. Final Answer:

    Data: file content -> Option B
  4. Quick Check:

    Null error means success, so data logs [OK]
Quick Trick: Null error means no error, print data [OK]
Common Mistakes:
  • Printing error when error is null
  • Confusing error and data values
  • Expecting no output due to async

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes