Pages

[JS] Understand Javascript event loop

Ever wonder how a single threaded language like Javascript can support concurrency? Of course, via asynchronous callback you will say. But how exactly does it work? or why setTimeout to zero does not enforce a function to be executed immediately?


console.log('Top');

setTimeout(function() {
  console.log('Middle');
}, 0); 

console.log('Bottom');

Philip Roberts had a great presentation, explaining how call stack, task queue and event loop work together. This 30-minute long video (you can also skip the first 10 minutes) can help you understand a lot about Javascript event loop.

You can also find his awesome simulation of Javascript event loop here.

No comments:

Post a Comment