by solarkraft · 3y ago · 0 comments
This part never really made it into my understanding of the concept. For practical purposes I've been completely fine with not thinking about it at all. My function is called at some point, that's all what I find really matters. Thinking about the event loop opens up a can of worms I find more confusing than useful.
Probably for daily usage this might be true, but there are definitely cases when you're using JavaScript and not understanding the event loop makes it hard to understand why things are happening. Example:
setTimeout(() => console.log('hello'), 0)
console.log('world')
You'd normally expect this to print "hello" and then "world" since the "sleep time" for the setTimeout is set to 0, but thanks to the event loop, the result will be "world" and "hello". Very simple example, but in real life code bases and beginner JavaScript developers, in can be confusing at times.I've seen smart and capable programmers who are not used to UI programming and async in general who struggle with callbacks and IoC initially. You can draw a simple picture on a whiteboard to explain the overall concept to give them something to grasp on.