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}`)
}
}