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

185
Visualizações
How to handle infinite loop error javascript

I have a auto-created JS file. And my module call it, some time the auto-created JS file cause infinite loop. And i will call thousand of auto-created JS file, some will run correct but some will cause infinite loop and I will don't know about file content
==> I wonder how I can handle this ?
Example of auto-created JS file. It name "excute.js"

const excuted = (a,b) => {
   let i = 0;
   while(a<b){
     i++;
   }
   console.log(i);
}

module.exports = excuted();

Example My module ==> i want handle infinite loop error here

const {exec} = require("child_process");

const check = () => {
  exec('node excute.js', (error, stdout, stderr) => {
    if(error) {
        console.log(error.message);
        return;
    }
    console.log(stdout);
  })
}

check();

I'm thinking about use setTimeout inside my module file or auto-created JS file. But i really get stuck and confused !!!. Thank every one

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

0

It is impossible to deterministically detect an infinite loop within another process. In your case you may simply want to timeout the process:

const {exec} = require("child_process");

async function check(fileName, timeOut = 1000) {
  return new Promise((resolve, reject) => {
    let _t = null;
    // Execute
    const process = exec(`node ${fileName}`, (error, stdout, stderr) => {
      if (_t) clearTimeout(_t);
      if (error) reject(error);
      else resolve(stdout);
    })
    // Time Out
    _t = setTimeout(() => {
      process.kill(-1);
      reject(undefined);
    }, timeOut);
  })
}

check()
  .then(stdout => console.log(stdout))
  .catch(console.error);

Side Note

Pay attention to exec() as it is executing code on your server. If the code contains errors (as in your case) nothing assures you those errors are not fatal for your system. Furthermore never execute untrusted code like that.

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