If I use setTimeout in a loop so only after the loop ends it executes all the operation in the same time. I tried to put setTimeout in a separate function, as suggested by many articles, but it does not help. The code is very simple:
function foo(i)
{
setTimeout( function() {console.log(i);}, 2000);
}
for (let i=0; i<5; i++)
{
foo(i);
}
It prints 0 1 2 3 4 in one shot
foofunction immediately 5 times. Or let's say, talk to JavaScript: Give me print out 0 in 2 secs, and 1 in 2 secs... So after 2 secs, you get the result one by one.