How to loop through data and wait after certain number of executions

Viewed 101

I'm trying to understand the algorithmic logic of executing some code a set number of times, but breaking the executions into a set number, then pausing for a time before executing the next set of times.

Should I be incorporating a Promise structure (async/await) into the code to improve my understanding?

In this code, at 500 executions, the algorithm should pause some set amount of time and then resume the next 500 executions.

It looks as though all the code executes, then the timeout is executed.

I'm confused though, the setTimeout() doesn't even wait 5 seconds.

Constructive criticism appreciated.

OUTPUT:

> node test.js

Execute code 1
Execute code 2
Execute code 3
.
.
.
Execute code 2398
Execute code 2399
        Execution 2400
Execute code 2401
Execute code 2402
.
.
.
Execute code 2598
Execute code 2599
        Execution 2600
Execute code 2601
Execute code 2602
.
.
.
Execute code 2654
Execute code 2655
i = 500 Resume at Thu Jul 15 2021 19:53:56 GMT-0700 (Pacific Daylight Time) 
i = 1000 Resume at Thu Jul 15 2021 19:53:56 GMT-0700 (Pacific Daylight Time) 
i = 1500 Resume at Thu Jul 15 2021 19:53:56 GMT-0700 (Pacific Daylight Time) 
i = 2000 Resume at Thu Jul 15 2021 19:53:56 GMT-0700 (Pacific Daylight Time) 
i = 2500 Resume at Thu Jul 15 2021 19:53:56 GMT-0700 (Pacific Daylight Time) 

ALGORITHM:

let reference = 2655
for (let i = 1; i <= reference; i++) {
  if (i % 500 === 0) {
    console.log(`counter = ${i}`)
    console.log(`Execute code ${i}`)
    setTimeout(() => {
      let time = new Date()
      console.log(`i = ${i} Resume at ${time} `)
    }, 50000)
  } else if (i % 100 === 0) {
    console.log(`\tExecution ${i}`)
  } else {
    console.log(`Execute code ${i}`)
  }
}

3 Answers

Maybe you can do it with an async function, promise and await. But the following will also work, using a simple setInterval():

const max=2655,m=500,t=5000;
var n=0;
const intv=setInterval(doit,t);
doit(); // do the first chunk immediately!

function doit(){
 for (let i=m;i--;){
  n++; // do whatever needs doing here ...
  if (n>=max) {
   clearInterval(intv);
   break;
  }
 }
 console.log(n,new Date());
}

The problem with your code was that you executed a number of setTimeout()calls within a loop at more or less the same time. The functions inside these calls were then executed in quick succession about 50 seconds after the main loop had already finished.

const data=[...new Array(2655)].map((_,i)=>i),m=500,t=5000;
var n=0, SUM=0;

const intv=setInterval(doit,t);
doit(); // do the first chunk immediately!

function doit(){
 data.slice(n,n=n+m).forEach(sum);
 // do whatever needs doing here ...
 if (n>=data.length) clearInterval(intv);
 console.log(Math.min(n,data.length),SUM,new Date());
}

function sum(v){SUM+=v}

How to make asynchronous code wait before continuing

When you use the async operator before a function, you turn it into an async function.

Inside an async function, you can use the await operator before asynchronous code to tell the function to wait for that operation to complete before moving on.

In this example, we’ve turned asyncFn() into an async function. We’ve also prefaced the window.fetch() call with the await operator.

async function asyncFn () {
    await fetch('https://jsonplaceholder.typicode.com/posts/').then(function (response) {
        return response.json();
    }).then(function (data) {
        console.log('Async Fetch', data);
    });
    console.log('Async Message');
}
asyncFn();

When this runs, Async Fetch and the returned data are logged into the console before Async Message. The function waited for the window.fetch() Promise to settle before continuing.

async function test(reference = 2655) {
  for (let i = 1; i <= reference; i++) {
    if (i % 500 === 0) {
      console.log(`counter = ${i}`);
      console.log(`Execute code ${i}`);

      // Wait five seconds
      await new Promise((res) => {
        setTimeout(() => {
          let time = new Date();
          console.log(`i = ${i} Resume at ${time} `);
          res();
        }, 5000);
      });

    } else if (i % 100 === 0) {
      console.log(`\tExecution ${i}`);
    } else {
      console.log(`Execute code ${i}`);
    }
  }
}

test();
Related