Random Dude
Random Dude

Reputation: 37

Javascript Pause Loop Until function is finished

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

Answers (1)

Beingnin
Beingnin

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

Related Questions