-2
var a = [1,2,3,4,5,6,7,8,9,10];

a.forEach(item => {
  ...
});
console.log('all done');

Is it ever possible in browser or node environment that it console 'all done' before the forEach completes its execution? If yes then when?

Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
Suman
  • 373
  • 4
  • 9

2 Answers2

0

No. Your code is synchronous so it can never happen unless you explicitly execute the .forEach function in an async block.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
0

No. forEach is a synchronous function.

It is possible to call asynchronous functions from within it, and they might not have completed, but the loop itself will have done.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335