Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda