Reputation: 37
I am trying to loop a function but I need the loop to pause, execute the function, then continue the loop. The function takes around 1 to 5 minutes to finish. Then I want to unpause the loop and redo the function 20 times.
What I've tried so far:
for(i = 0; i < 20; i++) {
//Some function here
}
do {
var i = 0
// Some function here
i++
} while (i < 20)
Upvotes: 2
Views: 1317
Reputation: 2422
If your function is asynchronous you could define the loop something like this to wait for the process to complete. For synchronous functions you dont have to do anything. The loop will automatically waits till it completes
async function doAll()
{
for(i = 0; i < 20; i++) {
await heavyDutyFn()//this function should return a promise always
}
}
Upvotes: 1