-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
  • 34,173
  • 19
  • 88
  • 85
Suman
  • 333
  • 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 朕會功夫
  • 88,688
  • 41
  • 174
  • 241
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
  • 857,932
  • 118
  • 1,152
  • 1,264