0

I have code like this:

function s() {console.log("in set time out")};

setTimeout(s, 100);
for (let i = 0; i < 50000; i++) {
 console.log(i);
};

This is output of console: enter image description here

As you see the function s ran only after the loop ends.

Please explain me if you have experience with this problem.

Thank you.

Huy Ha
  • 72
  • 5
  • 1
    Yes. This is because JavaScript is single threaded. http://latentflip.com/loupe/?code=ZnVuY3Rpb24gcygpIHtjb25zb2xlLmxvZygiaW4gc2V0IHRpbWUgb3V0Iil9OwoKc2V0VGltZW91dChzLCAxMDApOwpmb3IgKGxldCBpID0gMDsgaSA8IDUwMDAwOyBpKyspIHsKIGNvbnNvbGUubG9nKGkpOwp9Ow%3D%3D!!!PGJ1dHRvbj5DbGljayBtZSE8L2J1dHRvbj4%3D The callback queue seems to be executed after the callstack is empty – evolutionxbox Jun 14 '21 at 16:56
  • okay, thanks for explain – Huy Ha Jun 14 '21 at 17:03

0 Answers0