Why does a promise resolve before a `setTimeout` with a zero delay? The answer reveals the hidden priority inside JavaScript's event loop.
#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.
Matching moments
05:24 MIN
Demystifying asynchronous execution with the event loop
Multithreading in Javascript: A guide to Web Workers
05:57 MIN
Visualizing the JavaScript event loop and call stack
JavaScript the Grumpy Parts
04:53 MIN
The four pillars of high-performance JavaScript
Things I learned while writing high-performance JavaScript applications
01:57 MIN
Presenting live web scraping demos at a developer conference
Tech with Tim at WeAreDevelopers World Congress 2024
03:03 MIN
The origin and design philosophy of JavaScript
JavaScript the Grumpy Parts
03:46 MIN
Overcoming thread limitations with event-driven programming
Concurrency in Python
12:05 MIN
Understanding the fundamentals of event-driven systems
Event Messaging and Streaming with Apache Pulsar
06:05 MIN
Optimizing code by understanding the V8 runtime
Things I learned while writing high-performance JavaScript applications
Dev Digest 136 - No JS(on) of mineNews and ArticlesDouglas Crockford is our featured video, so let's talk about evolving JavaScript and all things JSON. Judicious JSON explains all the weird things in it, you can learn why it can be incredibly slow, people wonder what even is a JSON ...
Chris Heilmann
WeAreDevelopers LIVE days are changing - get ready to take partStarting with this week's Web Dev Day edition of WeAreDevelopers LIVE Days, we changed the the way we run these online conferences. The main differences are:Shorter talks (half an hour tops)More interaction in Q&AA tips and tricks "Did you know" sect...
Chris Heilmann
All the videos of Halfstack London 2024!Last month was Halfstack London, a conference about the web, JavaScript and half a dozen other things. We were there to deliver a talk, but also to record all the sessions and we're happy to share them with you. It took a bit as we had to wait for th...
Chris Heilmann
Processing 175 WeAreDeveloper World Congress talk videos in 5 hours - with PHP?Every year after the WeAreDevelopers World Congress is over, we have a ton of video footage to edit and release. Most of it is in raw format and needs editing by hand, but a lot of our sessions are also streamed live on YouTube and thus easier to re-...
From learning to earning
Jobs that call for the skills explored in this talk.