Bird
0
0

Why does this code never log the exit code?

medium📝 Debug Q7 of 15
Node.js - Child Processes
Why does this code never log the exit code?
const { spawn } = require('child_process');
const child = spawn('node', ['-e', 'process.exit(3)']);
child.on('exit', () => console.log('Exited'));
AThe exit event callback does not receive the exit code parameter
BThe child process never exits
CThe exit event is not emitted for process.exit()
DThe callback ignores the exit code parameter
Step-by-Step Solution
Solution:
  1. Step 1: Check the exit event callback signature

    The exit event callback receives the exit code as the first argument.
  2. Step 2: Identify missing parameter usage

    The callback does not declare a parameter, so the exit code is not logged.
  3. Final Answer:

    The callback ignores the exit code parameter -> Option D
  4. Quick Check:

    Declare parameter to access exit code [OK]
Quick Trick: Declare parameter in exit callback to get code [OK]
Common Mistakes:
  • Not declaring exit code parameter in callback
  • Assuming exit event won't fire on process.exit()
  • Thinking child never exits

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes