Bird
0
0

Which of the following is the correct way to listen for errors on a child process created with spawn?

easy📝 Syntax Q12 of 15
Node.js - Child Processes
Which of the following is the correct way to listen for errors on a child process created with spawn?
Achild.on('error', (err) => { console.error(err); });
Bchild.error((err) => { console.error(err); });
Cchild.on('exit', (code) => { console.log(code); });
Dchild.catch('error', (err) => { console.error(err); });
Step-by-Step Solution
Solution:
  1. Step 1: Recall the correct event listener syntax

    Node.js child processes use on method to listen for events like 'error'.
  2. Step 2: Identify the correct event and method

    child.on('error', (err) => { console.error(err); }); uses child.on('error', callback), which is the proper syntax to catch errors.
  3. Final Answer:

    child.on('error', (err) => { console.error(err); }); -> Option A
  4. Quick Check:

    Use on('error') to catch errors [OK]
Quick Trick: Use child.on('error', callback) to catch errors [OK]
Common Mistakes:
  • Using wrong method like .error() or .catch()
  • Listening to 'exit' instead of 'error' for errors
  • Missing parentheses or wrong event name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes