Whoop Whoop! Event Loop!
Last week a friend asked me, Is Node is multi-threaded? How would Node do asynchronous work if it’s not multi-threaded? Good question! I didn’t know the answer.
A stack, a queue, an event loop - oh my!
Image: Mozilla Developers Network (annotations mine)
timer or the infamous
XMLHttpRequest. (I didn’t realize those were separate APIs until I started learning about the event loop!) When a message is processed, its callback function gets called and thereby is added to the stack.
The event loop is the loop that processes messages in the queue such that the callback functions get pushed onto the stack.
For an animated demo of how the event loop works in the browser, check out Philip Roberts' 2014 JSConf talk.
Image: Philip Roberts, 2014
- JSConf 2014: What the heck is the event loop anyway?
- Concurrent model and Event loop - MDN
- a knowledgeable coworker who explained aynchronicity and Node