Reputation: 537
I'm trying to verify the order in which the following code happens.
function square(n) {
return n * n;
}
setTimeout(function(){
console.log("Hello");
}, 0);
console.log(square(2));
setTimeout() is popped off the stack, and then anonymous() goes to the queue.
While setTimeout() is on the stack, anonymous() goes to the queue, and then setTimeout() is popped off the stack.
Which of the above is the correct order? I tried it on this link and what I've noticed is that setTimeout() is popped of first and then anonymous() goes to the queue but I just need to verify this.
Upvotes: 0
Views: 158
Reputation: 88
The first is answer is correct. The setTimeout function gets popped off then anonymous(), the inner unnamed function written as the first argument in the setTimeout, is sent to the queue and will remain there, until all other code runs;
Upvotes: 2