Challenge - 5 Problems
Child Process Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a child process emits an 'error' event?
Consider this Node.js code snippet that spawns a child process. What will be logged if the child process fails to start due to an invalid command?
Node.js
import { spawn } from 'child_process'; const child = spawn('invalid_command'); child.on('error', (err) => { console.log('Error event:', err.message); }); child.on('exit', (code) => { console.log('Exit event with code:', code); });
Attempts:
2 left
💡 Hint
The 'error' event is emitted when the process cannot be spawned or killed.
✗ Incorrect
When the child process fails to start (e.g., invalid command), the 'error' event is emitted with an error object describing the failure. The 'exit' event is not emitted because the process never started.
❓ state_output
intermediate2:00remaining
What is the value of 'exitCode' after a child process error?
Given the following code, what will be the value of 'exitCode' after the child process emits an 'error' event?
Node.js
import { spawn } from 'child_process'; let exitCode = null; const child = spawn('badcmd'); child.on('error', () => { exitCode = 'error_occurred'; }); child.on('exit', (code) => { exitCode = code; });
Attempts:
2 left
💡 Hint
Check which event sets the exitCode when the process fails to start.
✗ Incorrect
Since the process fails to spawn, the 'error' event fires and sets exitCode to 'error_occurred'. The 'exit' event does not fire, so exitCode remains 'error_occurred'.
📝 Syntax
advanced2:00remaining
Which option correctly attaches an error handler to a child process?
Identify the correct syntax to handle errors from a spawned child process.
Node.js
import { spawn } from 'child_process'; const child = spawn('ls');
Attempts:
2 left
💡 Hint
Node.js child processes use EventEmitter pattern.
✗ Incorrect
The correct method to listen for events on child processes is 'on'. Other methods like 'error', 'addEventListener', or 'handle' do not exist on the child process object.
🔧 Debug
advanced2:00remaining
Why does this child process error handler never run?
Examine the code below. Why does the error handler never log anything even if the command is invalid?
Node.js
import { spawn } from 'child_process'; const child = spawn('invalidcmd'); child.on('exit', (code) => { console.log('Exited with code:', code); }); child.on('close', (code) => { console.log('Closed with code:', code); });
Attempts:
2 left
💡 Hint
Check if the code listens for the 'error' event.
✗ Incorrect
The 'error' event is emitted when the child process fails to start. Since no listener is attached for 'error', the error is unhandled and the handler never runs.
🧠 Conceptual
expert3:00remaining
What is the correct sequence of events when a child process fails to spawn?
Order the events emitted by a Node.js child process when the spawn command is invalid and fails immediately.
Attempts:
2 left
💡 Hint
Consider which events fire when the process never starts.
✗ Incorrect
When spawn fails immediately, only the 'error' event is emitted. The 'spawn', 'exit', and 'close' events do not fire because the process never started.