Complete the code to listen for the child process exit event.
childProcess.on('exit', (code) => { console.log('Child exited with code', [1]); });
The 'exit' event provides the exit code as the first argument, which is commonly named code.
Complete the code to spawn a child process running 'ls' command.
const { spawn } = require('child_process');
const ls = [1]('ls', ['-lh', '/usr']);The spawn function is used to launch a new process with a given command and arguments.
Fix the error in the code to correctly check if the child process exited normally.
childProcess.on('exit', (code, signal) => { if ([1] === null) { console.log('Exited normally with code', code); } else { console.log('Process killed by signal', signal); } });
The signal parameter is null if the process exited normally. Checking if signal === null tells us if the exit was normal.
Fill both blanks to create a dictionary of exit codes and their meanings.
const exitCodes = {
0: [1],
1: [2]
};Exit code 0 means success, and exit code 1 usually means a general error.
Fill all three blanks to create a filtered object of exit codes greater than zero.
const filtered = Object.fromEntries( Object.entries(exitCodes).filter(([[1], [2]]) => [3] > 0) );
We destructure the entries into code and message. We convert code to a number to compare it with 0.