-1

Suppose I have a forloop that does some execution. I would like to break the for loop if it takes more than 20 seconds to execute.

async function mainFunc(){

    for (let step = 0; step < 5; step++) {
        // Runs some complex operation that is computationally intensive
        //someFunc() is a async method. SomeAnotherFunc() is a synchronous one.
        await someFunc();
        someAnotherFunc();//this function contains built in function 
        // execSync() that runs command line functions 
        await someFunc(); 
 
    }
}

Can anyone provide me a simple solution where tracking the time and breaking the loop happens on a separate thread so that I don't burden my existing for loop execution?

This answer partially works for me. The solution given by @Molda will only help me in the case of Async methods. If you see the code snippet that I provided, there is a synchronous method called someAnotherFunc() which has a nodejs process function "execSync" that runs command line functions. Now it is actually a pain point for me to track the time of this function as I dont have access to the method as it is a built in function. I would appreciate on your suggestions on how to proceed further.

Molda
  • 5,619
  • 2
  • 23
  • 39
user9683713
  • 185
  • 2
  • 13
  • JS is single threaded – Tushar Shahi Dec 06 '21 at 13:25
  • What have you tried? How does your `console.log` even take 20 seconds? – GOTO 0 Dec 06 '21 at 13:25
  • The code that I have shown inside the for loop is not the actual code. Inside the for loop, there's a method that performs some operation for processing some data and this processing takes more than 20 seconds on certain occasions. – user9683713 Dec 06 '21 at 13:27
  • Although I use Async method for the processing, I am supposed to end this batch processing in 2p seconds and indicate the "Time limit" exceeded if it takes more than 20 seconds to execute. – user9683713 Dec 06 '21 at 13:29
  • So how does your async method look like? The code example you are showing is synchronous. – GOTO 0 Dec 06 '21 at 13:31
  • @GOTO you can now have a look at the edited question. As it is a customer proprietary code, I just have given the abstract details and I should have framed the question in a better way first itself. Sorry for the confusion though. – user9683713 Dec 06 '21 at 13:40
  • `some operation for processing some data` is this sync, if so the accepted answer using a timeout won't help.. – Keith Dec 06 '21 at 13:49
  • @user9683713 If the accepted answer works for you, then it's not your asynchronous code that takes too long to run and needs to be stopped, but simply your loop that does too many iterations. – GOTO 0 Dec 06 '21 at 13:58
  • @Keith you are actually right. This answer partially works for me. The solution given by will only help me in the case of Async methods. If you see the code snipped that I provided, there is a synchronous method called someAnotherFunc() which has a nodejs process function "execSync" that runs command line functions. Now it is actually a pain point for me to track the time of this function as I dont have access to the method as it is a built in function. I would appreciate on your suggestions on how to proceed further. – user9683713 Dec 06 '21 at 14:14
  • 1
    `execSync`, that's always going to be an issue, I would use the `exec`, version. You can easily convert this to a promise and then the timeout will work again. It's even possible to then call `kill` on the process too, if the 20 seconds has passed. – Keith Dec 06 '21 at 14:29
  • 1
    @Keith Finally the issue is resolved after I followed your suggestion. Used Exec instead of Execsync and used promise along with it and I wrapped it within a timeout function. Looks like a neat solution. Thank you very muc – user9683713 Dec 07 '21 at 13:46

2 Answers2

2

That should be fairly simple

var stop = false;
setTimeout(()=>{ stop = true; }, 20 * 1000);

for (let step = 0; step < 5; step++) {
    if (stop) break;
}

Note:

The for loop without async/await is synchronous which means even calling multiple requests/db calls/ etc. will only take less then a second for the for loop to finish. So i can't imagine what you would do in it that would take 20 seconds

Molda
  • 5,619
  • 2
  • 23
  • 39
  • @user9683713 well it's quite simple solution so some people may think you haven't tried hard enough to figure this by yourself. I'm glad i could help. – Molda Dec 06 '21 at 13:33
  • Well that makes sense now. Anyways thanks a lot. – user9683713 Dec 06 '21 at 13:34
  • could you please check my edited details again, your answer partially works as the function I am trying to track has built in functionalities. This is why I am struggling to find a solution although it looks very simple. – user9683713 Dec 06 '21 at 14:17
  • Well, unless you can show me the code of someAnotherFunc i can't help you. If it only contains execSync then there should be no problem since it is synchronous as the name suggests. – Molda Dec 06 '21 at 17:00
  • well I used Exec along with promise and wrapped it within a timeout function and this worked well. – user9683713 Dec 07 '21 at 13:47
0

Have you tried to add a timer inside the for loop and when it finish the 20 seconds to break?

How to do a timer in Angular 5
https://www.w3schools.com/js/js_break.asp

Something like this:

var stopTimer = false;
setTimeout(() => {
    stopTimer = true;
}, 20 * 1000);

for (let step = 0; step < 5; step++) {
    if (stopTimer) break;
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Vitomir
  • 335
  • 2
  • 15
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 06 '21 at 13:29