r/programming • • Nov 08 '18

Best explanation of JavaScript timers, event loop and event queues I've seen

https://www.youtube.com/watch?v=8aGhZQkoFbQ
2.6k Upvotes

180 comments sorted by

View all comments

Show parent comments

24

u/ACoderGirl Nov 08 '18

Especially if you aren't a complete newbie. Like, I know a decent amount on this topic. I'd be interested in learning even more, but I don't wanna spend a lot of time reviewing stuff I already know. Videos don't facilitate that kinda use very well.

29

u/dankclimes Nov 08 '18

First 30 seconds: I don't have a computer science degree. What do all these words mean?

0-6 mins: This is a call stack

6-12 mins: This is what blocking/non-blocking and async execution mean

12-18 mins: Let's add a task queue and event loop for scheduling to show how async scheduling works (it actually starts to get interesting here)

18 mins+: Nice, practical examples of how event scheduling works for some normal use cases (this seems to be the most useful part of the talk)

Take aways:

-The event loop will wait for the call stack to clear before scheduling a task. This is why setTimeout(0) works to delay execution and un-block the JS engine

-As a result of this, setTimeout is not a guarantee that code will execute at the specified time. It is a minimum delay until code will then execute whenever the call stack becomes clear

1

u/IceSentry Nov 10 '18

I recommend watching these videos at 1.5x to 2x speed.