Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Event Loop

So the Event Loop ONLY pushes callback functions to the call stack when the call stack is Completely empty? That seems wrong and/or inefficient, and yet that's what the wording implies in this instruction

2 Answers

Brandon White
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brandon White
Full Stack JavaScript Techdegree Graduate 35,773 Points

Hi Joseph,

That’s essentially correct. The event loop can only move a function from the queue to the stack when the stack is empty.

And since callback functions are asynchronous, they’re moved from the browser to the queue. And that’s where they wait until the stack is ready.

Thanks Brandon... This stackoverflow thread also helped me out a lot: [https://stackoverflow.com/questions/52906975/call-stack-event-loop-why-waiting-for-empty-stack]

All callback functions are pushed into the callback queue even if call stack is empty. The event loop pops the callback functions one by one from the callback queue and transfers them to call stack for execution. Read how the event loop and callback queue work: https://www.datainfinities.com/10/event-loop-and-callback-queue-in-javascript