Christian Woerz

The Eventloop in JavaScript - How does it work?

Why does a promise resolve before a `setTimeout` with a zero delay? The answer reveals the hidden priority inside JavaScript's event loop.

The Eventloop in JavaScript - How does it work?
#1about 4 minutes

Differentiating between a JavaScript engine and a runtime

A JavaScript engine implements the ECMAScript standard, while a runtime adds extra functionality like Web APIs and the event loop.

#2about 2 minutes

Understanding why JavaScript needs an event loop

JavaScript's single-threaded nature can block the UI, so the event loop is necessary to handle asynchronous operations without freezing the application.

#3about 7 minutes

Exploring the core components of a JavaScript runtime

A runtime consists of a call stack for synchronous code, Web APIs for browser or Node features, and separate queues for microtasks and macrotasks.

#4about 2 minutes

How the event loop prioritizes and executes tasks

The event loop continuously checks if the call stack is empty, then processes all available microtasks before handling a single macrotask.

#5about 4 minutes

Demonstrating setTimeout and the macro task queue

Code example shows how `setTimeout` with a zero delay is placed in the macrotask queue and executed only after the synchronous call stack is clear.

#6about 2 minutes

Prioritizing promises with the micro task queue

A resolved promise is handled as a microtask, which is always executed before macrotasks like `setTimeout` when the call stack is empty.

#7about 3 minutes

How network latency affects promise execution order

Using `fetch`, this example shows that a promise is only added to the microtask queue upon resolution, so a slow network request can execute after a faster macrotask.

#8about 3 minutes

Starving the macro task queue with micro tasks

A recursive `queueMicrotask` call demonstrates how continuously adding microtasks can prevent the event loop from ever processing the macrotask queue.

#9about 2 minutes

Final recap of the event loop's execution order

The event loop prioritizes the call stack first, then the entire microtask queue, and finally a single task from the macrotask queue.

Related jobs
Jobs that call for the skills explored in this talk.

Featured Partners

Related Articles

View all articles
DC
Daniel Cranney
Dev Digest 195: End of Likes, JavaScript’s a Zoo, and Messing with Bots!
Inside last week’s Dev Digest 195 . 👎 No more external likes 🤗 Needy programs 📉 The worst selling Microsoft product 🟨 JavaScript engines zoo 🍞 No more toasts! 🤖 Messing with bots 👔 Beware of fake job interviews 🗞️ Join over 150,000 developers alread...
Dev Digest 195: End of Likes, JavaScript’s a Zoo, and Messing with Bots!
CH
Chris Heilmann
Links and anchors: ancient HTML and JS magic in your browser
Have you ever wondered why you use an <a> element to add link to an HTML document? Paragraph is <p>, unordered list is <ul>, image is <img> but why <a> instead of <link>? Well, the reason is that the element both defines where to go to, but also an e...
Links and anchors: ancient HTML and JS magic in your browser
DC
Daniel Cranney
How to Avoid Over-Engineering
In today’s software development world, the demand for designing applications that are both robust and easy to maintain is more pressing than ever. Many developers encounter the architectural chaos left behind in older codebases, leading to frustratio...
How to Avoid Over-Engineering

From learning to earning

Jobs that call for the skills explored in this talk.

Creative Javascript

Creative Javascript

Thegiglab
Amsterdam, Netherlands

Intermediate
API
CSS
HTML
jQuery
Bootstrap
+1
JavaScript Developer

JavaScript Developer

Berkeley Square IT
Municipality of Madrid, Spain

Remote
JavaScript
Continuous Integration