-
Notifications
You must be signed in to change notification settings - Fork 502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question, not issue :) #17
Comments
Thank you in advance if somebody will help me to understand 😃 |
|
@phamvuau In demo it's empty: Check latenflip Does the engine add all of the synchronous code instructions from main thread(or main function which wraps the code) to some "virtual queue" which passes this all instructions step by step to call stack and after only running all such instructions from this "queue" event loop will pass functions from callback queue to call stack? |
@AndriyYakymiv it's empty in my side. I'm using Chrome latest.
There is a |
@phamvuau |
@AndriyYakymiv I think the best way to think of it is like this, and perhaps loupe should actually show this: instead of this function f() {
console.log("foo");
setTimeout(g, 0);
console.log("foo again");
}
function g() {
console.log("bar");
}
function b() {
console.log("bye");
}
f();
/*<---- Why the stack is not empty here, after f() function removing from stack? */
b(); think about it like this: function main() {
function f() {
console.log("foo");
setTimeout(g, 0);
console.log("foo again");
}
function g() {
console.log("bar");
}
function b() {
console.log("bye");
}
f();
b();
}
main() so think of your whole script as if it was wrapped in a big function. Then you would have a |
The JS runtime still executing the main function so it simply moves on next statement. If you put a break point in Chrome dev tool, you can see the call stack and the anonymous function which is the entry point for the execution. |
@phamvuau @latentflip |
As it is written in the MDN's article: When the stack is empty, a message is taken out of the queue and processed.
But I've noticed strange thing which I don't understand.
For example:
Output is next and it is correct order:
But why the
g()
function insetTimeout
runs only afterb()
function despite the fact that call stack is empty? Or it's not? In your demo it's emptyAnother example based on thoughts from previous:
So can I presume that if I have a
Promise
it means that.then()
part with resolve or error will run only after all synchronous code will finish because it won't be added to the call stack from callback queue because it's not empty? Or it is?That's why I have a question: when exactly call stack becomes fully empty?
The text was updated successfully, but these errors were encountered: