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

114
Visualizações
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 à 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