Dart Async Intro
Dart Async Intro
Some tasks take time to finish.
For example, reading a file, downloading data from the internet, or waiting for a timer.
If a program stops and waits for each slow task, the whole program freezes.
In an app, this means that buttons and animations stop working while the program waits.
Asynchronous code (often called async code) solves this problem.
Async code starts a slow task, lets the program do other work, and comes back when the task is done.
Synchronous Code
The code you have seen so far is synchronous.
Synchronous means that Dart runs one statement at a time, from top to bottom, and each statement must finish before the next one starts:
Example
void main() {
print('Boil water');
print('Make tea');
print('Drink tea');
}
Try it Yourself »
Result:
Boil water
Make tea
Drink tea
The output always comes in the same order as the code.
Waiting Without Blocking
Future.delayed() runs some code later, after a waiting time.
The waiting time is a Duration, for example Duration(seconds: 1).
Look at the order of the output:
Example
void main() {
print('Order a pizza');
Future.delayed(Duration(seconds: 1), () {
print('The pizza has arrived');
});
print('Watch a movie while you wait');
}
Try it Yourself »
Result:
Order a pizza
Watch a movie while you wait
The pizza has arrived
Dart did not stop at Future.delayed().
It started the timer, printed the last line at once, and ran the delayed code one second later.
The program only ends when there is no more waiting work left.
The Event Loop
Dart runs your code on a single thread.
A thread is a worker that runs code, one step at a time.
To handle waiting tasks, Dart uses an event loop.
You can think of the event loop as a to-do list:
- First, Dart runs all the normal code in
main(), from top to bottom. - When a slow task (like a timer) is finished, its code is added to the list.
- When Dart has nothing else to do, it takes the next item from the list and runs it.
This is why the delayed code always runs after the normal code, even with a waiting time of zero:
Example
void main() {
print('Start');
Future.delayed(Duration.zero, () {
print('Delayed code');
});
print('End');
}
Try it Yourself »
Result:
Start
End
Delayed code
Order of Output
When you have many delayed tasks, the shortest waiting time finishes first.
The order in the code does not matter, only the waiting time does:
Example
void main() {
Future.delayed(Duration(seconds: 3), () {
print('Bus 3 arrived after 3 seconds');
});
Future.delayed(Duration(seconds: 1), () {
print('Bus 1 arrived after 1 second');
});
Future.delayed(Duration(seconds: 2), () {
print('Bus 2 arrived after 2 seconds');
});
print('Waiting for the buses...');
}
Try it Yourself »
Result:
Waiting for the buses...
Bus 1 arrived after 1 second
Bus 2 arrived after 2 seconds
Bus 3 arrived after 3 seconds
All three timers run at the same time, so the program takes about 3 seconds in total, not 6.
In the next chapters, you will learn the tools Dart gives you for async code:
- Futures: a value that will be ready later
- async and await: a simple way to write code that waits for futures
- Streams: many values that arrive one after another