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.