Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!