Bird
0
0

You wrote this code to spawn a child process and listen for exit:

medium📝 Debug Q14 of 15
Node.js - Child Processes
You wrote this code to spawn a child process and listen for exit:
const { spawn } = require('child_process');
const child = spawn('node', ['-e', "setTimeout(() => process.exit(0), 1000)"]);
child.unref();
child.on('exit', (code) => {
  console.log(`Exited with code ${code}`);
});

But the console never logs anything. What is the likely problem?
AThe exit event callback is missing the signal parameter.
BThe child process is not exiting because the event loop is blocked.
CThe child process exits after 1 second, but the main program ends before that.
DThe spawn command syntax is incorrect.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the child process behavior

    The child process exits after 1 second due to setTimeout.
  2. Step 2: Consider the main program lifecycle

    If the main program ends before 1 second, it may exit before child finishes, so no log appears.
  3. Final Answer:

    The child process exits after 1 second, but the main program ends before that. -> Option C
  4. Quick Check:

    Main program must stay alive to see child exit [OK]
Quick Trick: Keep main program alive to catch delayed child exit events [OK]
Common Mistakes:
  • Thinking missing signal parameter breaks event
  • Assuming spawn syntax is wrong without error
  • Ignoring asynchronous timing of child exit

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes