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();
callstack here carry on one task then remove it, so why callback hasn't executed after popping out instanctly?
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I have question here...
callstack here carry on one task then remove it, so why callback hasn't executed after popping out instanctly?
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.