The Event Loop and Microtasks
Understand how Dart schedules async work.
The Event Loop and Microtasks is a free Dart Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
One Thread, No Waiting
Dart runs your code on a single thread, yet it never freezes. The secret is the event loop, which schedules work instead of blocking. ⚡
What the Event Loop Does
The event loop is a simple cycle: pick the next pending task, run it to completion, then repeat. Your code runs in tidy, uninterrupted chunks.
Synchronous First
All your plain, top-to-bottom code runs first. Only when that synchronous work finishes does the loop start handling scheduled tasks.
Two Queues
The loop pulls from two lines: the microtask queue and the event queue. Microtasks always jump ahead of events.
The Microtask Queue
A microtask is tiny deferred work, like a completed Future callback. The loop drains every microtask before touching the event queue.
The Event Queue
The event queue holds bigger items: timers, file reads, taps, and network responses. They wait until all microtasks are done.
Scheduling a Microtask
You can hand work to the microtask queue with scheduleMicrotask. It runs after the current code but before any event.
scheduleMicrotask(() => print("microtask"));
print("right now");Scheduling an Event
A Timer (or Future.delayed with zero) puts a callback on the event queue, so it runs after every pending microtask.
Timer.run(() => print("event"));
print("right now");Microtasks Win
If both are queued, the microtask runs first. This ordering keeps Future chains snappy and predictable.
scheduleMicrotask(() => print("A"));
Timer.run(() => print("B"));
// prints A then BWhy It Stays Smooth
Because each task runs to completion, your app never half-updates. The loop keeps everything responsive without manual thread juggling.
Do Not Starve the Loop
Flooding the microtask queue can block events forever, freezing input. Keep microtask work short and let events get their turn.
Quick Check
Quick check on queue ordering.
Recap: The Event Loop
You learned the event loop runs sync code first, then drains microtasks, then events. That ordering keeps Dart single-threaded yet smooth. 🎉
Frequently asked questions
Is the “The Event Loop and Microtasks” lesson free?
Yes — the full text of “The Event Loop and Microtasks” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “The Event Loop and Microtasks”?
Understand how Dart schedules async work. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Event Loop and Microtasks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Event Loop and Microtasks
- Creating and Awaiting Futures
- Error Handling in async Code
- Future.wait and Parallel Work