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

113
Vistas
How to prevent a infinite loop in nodejs child process spwan

I'm building an online ide by running the command to execute the code by spawn... but what should I do if there is an infinite loop in the code...The process is not being stopped

const { childElements } = require("dom-helpers");
const path = require("path");

const executeCpp = (filepath) => {
  const file = path.basename(filepath[1].split(".")[0]);

  const execute = new Promise((resolve, reject) => {
    const run = spawn(
      `cd ${filepath[0]} && g++ ${file}.cpp -o ${file}.out && ./${file}.out < ${file}.txt`,
      {
        shell: true,
      }
      // (error, stdout, stderr) => {
      //   error && reject({ error, stderr });
      //   stderr && reject(stderr);
      //   resolve(stdout);
      // }
    );
    setTimeout(() => {
      run.kill();
    }, 2000);

    run.on("exit", (code) => {
      // clearTimeout(to);
      resolve(`Process Exited with code ${code}`);
    });
    run.stderr.on("data", (data) => {
      resolve(String(data));
      console.log(data)
    });
    run.stdout.on("data", (data) => {
      resolve(data);
      console.log(data)
    });

  });

  return execute;
};
module.exports = {
  executeCpp,
};

This is my workaround, but it is not working. Also, when I use exec, I can reject the promise if there is an error, but it's not working; instead, I need to resolve the promise even if there is an error.

about 4 years ago · Juan Pablo Isaza
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