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

103
Visualizações
Synchronize spawn and stream event output NodeJS

I want to run set of test scripts using NodeJS, It outputs test results while executing. I want these different tests scripts to run synchronously and also output the results while execution.

I tried using the spawnSync() function of child process, however it doesn't output data during execution of the command.

const { spawnSync } = require("child_process")
const cmd = spawnSync('./gradlew',['test'],{cwd : '../../../'}) 
console.log(`Output: ${cmd.stdout}`)

The above code outputs the results only when the task has fully completed execution. I believe this is the expected behavior of spawnSync.

Therefore i tried running spawn() inside an async function with await to synchronize it, I'm not sure whether this approach is correct.

const { spawn } = require("child_process")

const cmd = spawn('./gradlew',['test'],{cwd : '../../../'})
const cmd1 = spawn('./gradlew',['test'],{cwd : '../../../'})

async function runFeature(){
   console.log("Running feature....")
   cmd.stdout.on('data', (data) => {
       console.log(`Output: ${data}`);
   });
}

async function runFeature1(){
   console.log("Running feature 1....")
   cmd1.stdout.on('data', (data) => {
       console.log(`Output1: ${data}`);
   });
}

async function main(){
   try{
       console.log("Starting...")
       await runFeature()
       await runFeature1()
       process.exit(0)
   } catch(e){
       console.error(e)
       process.exit(1)
   }
}

main()

When running the above code, the command ends without any output as follows. It looks like the cmd.stdout.on() method is not getting executed.

harrym@HarryM:~/Project/test$ node run-test.js
Starting..
Running feature....
Running feature 1....
harrym@HarryM:~/Project/test$

Is there anything wrong in this way of approach?. I really need help in debugging this issue. In addition any suggestions to synchronize a child process while outputting live data stream would be highly appreciated.

Thank you in advance :)

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

0

If you use node 10 or above, you can use generic iterator function like that:

const { spawn } = require('child_process');

(async () => {
  try {
    const lsSpawn = spawn('ls', ['-l'], { cwd: '../../../' });

    let error = '';
    for await (const chunk of lsSpawn.stderr) {
      error += chunk;
    }
    if (error) {
      console.error('error', error);
      return;
    }

    let data = '';
    for await (const chunk of lsSpawn.stdout) {
      data += chunk;
    }
    if (data) {
      console.log('data', data);
    }
  } catch (e) {
    console.error('execute error', e);
  }
})();

I use for await in lsSpawn.stderr and lsSpawn.stdout

Tip: If you wanna use promise don't mix-up with event.on, you should convert event.on to promise format

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