Javascript Event Loop
💻Programming Language
javascript
theory
Participants: Event stack, web APIs, task queue, event loop
Javascript runs an event loop (greatly explained in this YouTube video) in which functions can be registered to run in the next iteration of the never ending single-threaded loop.
-
setTimeout(..., 0)deferes code in the callback function to be executed when the event stack is empty, e.g.:setTimeout(function() { console.log('hello') }, 0)
Synchronous code
- Is called in the current iteration is blocking the event loop.
- Code which is not called as a call-back from another function (i.e. registered to be executed in the next iteration)
Asynchronous code
- Registers an
event handler(a callback routine) and doesn’t block the event loop. - With callbacks Javascript is concurrent out of the box.
Discuss on Twitter ● Improve this article: Edit on GitHub