Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
Node js Child process event emitter vs callbacks

Is there any difference between attaching callbacks or event listeners for child process in nodejs. like -

const execute = require('child-process').exec;
const process = execute('ping -n 1 www.google.com'); // or ping -c 1 www.google.com for mac

process.stdout.on('data', data => {
    console.log(data)
})  

In the above code, I am using an event listener for the output and I am getting stdout data in windows but can't get the output in macOS. But if I use callback like -

const execute = require('child-process').exec;

execute('ping -c 1 www.google.com', (error, stdout, stderr) => {
   console.log(stdout);
})

I am getting the output data in both windows and mac. Is there any difference using callback or event listeners(both are asynchronous)?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The callback is called when the execution of that async task is completed. However, the events have to be fired based on the observer. Every event has listeners and when an event is fired its related listener function starts the executions.

  • You can attach multiple listeners to the same event. Callbacks are one-to-one notifications, events - one-to-many.
  • You can't return value from the event. Events are one-way messages. Often, callbacks follow (error, data1, data2, data3, ...) signature because a single callback responsible for normal and error data flow (and async libraries usually expect this behaviour)
  • EventEmitter-based API, on the other hand, tend to separate error and non-error messages
  • "error" event is special in event emitter: if there is no listener for it, EventEmitter throws an exception. With callbacks, it's your responsibility to check the first error parameter.

You can check this link on stackoverflow for difference b/w callback and events.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda