-1

First example from https://nodejs.org/dist/latest-v5.x/docs/api/child_process.html is as follows:

const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
    console.log(`stderr: ${data}`);
});

ls.on('close', (code) => {
    console.log(`child process exited with code ${code}`)
});

Isn't it wrong as in fact the process (in this case ls) may end up before one starts reacting to events so basically one will miss them.

baao
  • 71,625
  • 17
  • 143
  • 203
mrs
  • 11

1 Answers1

0

No. While ls is a "blocking" process itself, the spawning of a childprocess in node is async, i.e. the above code spins up a new process, registers the event handlers and then when the process is ready to run commands it'll execute ls.

Creynders
  • 4,573
  • 20
  • 21