Sub topic 2
How the Event Loop Orders Your Code
This chapter assumes you know basic Dart syntax, functions, callbacks, and how to run a program with dart run. You should also know that a Future<T> represents a result that may arrive later.
Synchronous code runs first
Dart applications commonly use one isolate with one thread of execution. An isolate has its own memory and event loop. The event loop checks for work and runs one callback at a time.
Synchronous code runs immediately. Dart does not pause the current function when it starts an asynchronous operation. Instead, the operation schedules a callback for later and the current code continues.
import 'dart:async';
void main() {
print('1: main starts');
Future(() {
print('4: event callback');
});
scheduleMicrotask(() {
print('3: microtask callback');
});
print('2: main ends');
}Output:
1: main starts
2: main ends
3: microtask callback
4: event callbackThe Future constructor schedules its callback on the event queue. scheduleMicrotask adds its callback to the microtask queue. The two queues do not have the same priority.
Microtasks run before events
After the current synchronous code finishes, Dart drains the microtask queue. It runs every pending microtask before it takes the next item from the event queue.
The event queue contains larger units of asynchronous work, such as timer callbacks, input events, and many future callbacks. Once the microtask queue is empty, the event loop takes one event and runs it. It then checks the microtask queue again.
A microtask can add another microtask. Dart continues draining the queue until it is empty.
import 'dart:async';
void main() {
print('start');
scheduleMicrotask(() {
print('microtask 1');
scheduleMicrotask(() => print('microtask 2'));
});
Future(() => print('event'));
print('end');
}The output is:
start
end
microtask 1
microtask 2
eventWhy queue choice matters
Microtasks are useful for short work that should happen before the next event. They are not a general-purpose background thread. If code keeps adding microtasks without allowing the queue to empty, event callbacks can be delayed. This is called microtask starvation.
Use normal asynchronous operations, such as timers or I/O APIs, when work can wait for a later event. Use microtasks for small scheduling tasks, such as completing a result after the current synchronous call finishes.
| Code | Usual queue | When it runs |
|---|---|---|
| `scheduleMicrotask(callback)` | Microtask queue | After current synchronous code, before the next event |
| `Future(callback)` | Event queue | After pending microtasks |
| `Future.delayed(duration, callback)` | Event queue | After the duration and pending microtasks |
| `print(...)` | None | Immediately, synchronously |
A queue does not make CPU-heavy code non-blocking. A large loop still occupies the isolate while it runs. The event loop cannot process other callbacks until that loop finishes.
Reading execution order
When debugging asynchronous code, mark each statement by when it is scheduled:
- Run all current synchronous statements.
- Drain the microtask queue completely.
- Run one event callback.
- Drain any new microtasks.
- Continue with the next event.
This model explains why adding await, a timer, or a microtask can change the visible order of output. The operation may not finish immediately, but the current synchronous section always continues until it returns control to the event loop.
Exercise
In what order does this program print its labels: A, B, C, or D?
Show answer
It prints A, D, C, then B. A and D are synchronous, C is a microtask, and B is an event callback.