Bird
0
0

Identify the error in this code snippet that tries to catch a Promise rejection:

medium📝 Debug Q14 of 15
Node.js - Error Handling Patterns
Identify the error in this code snippet that tries to catch a Promise rejection:
fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(console.error());
AThe <code>response.json()</code> call should be inside <code>catch</code>
BThe <code>then</code> methods are missing error handlers
CThe <code>fetch</code> function does not return a Promise
DThe <code>catch</code> method is called with the result of <code>console.error()</code> instead of a function
Step-by-Step Solution
Solution:
  1. Step 1: Check how catch is used

    The code calls console.error() immediately and passes its result (undefined) to catch, which expects a function.
  2. Step 2: Understand correct catch usage

    catch should receive a function reference or arrow function, like catch(error => console.error(error)).
  3. Final Answer:

    The catch method is called with the result of console.error() instead of a function -> Option D
  4. Quick Check:

    Pass function to catch, not call it [OK]
Quick Trick: Pass function to catch, don't call it immediately [OK]
Common Mistakes:
  • Calling console.error() inside catch instead of passing function
  • Ignoring that fetch returns a Promise
  • Misplacing response.json() call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes