DEV Community

Discussion on: ✨♻️ JavaScript Visualized: Event Loop

Collapse
 
karim_muhammad profile image
Karim Muhammad • Edited

I have question here...

function hello() { // sync
console.log("hello");
}
function afterWhile() { async
  setTimeout(()=> {
    console.log("After Timer")
  }, 0)
}
afterWhile();

hello();  // gets added in callstack then pop it out sequentally;
// so callstack is empty here, i guess?!!!! so why callback hasn't been executed here, in this time

hello();
Enter fullscreen mode Exit fullscreen mode

callstack here carry on one task then remove it, so why callback hasn't executed after popping out instanctly?

Collapse
 
jeannotmn profile image
Jeannot MN

Functions are pushed into the the call stack when the callstack is empty AND there are no other line of code to be executed in the main thread... in your case it was not pushed because there was still one more thing to execute in the main thread of execution (the second hello()); Only after completing the second call to the hello function that your callback will be pushed into the callstack...

I hope this helps.