Asynchronous Programming in Dart
Asynchronous Programming teaches you to run slow work without freezing your program.
In this chapter, you will:
- understand the event loop, and wait for Futures with
asyncandawait - handle many values over time with Streams
- run heavy work in the background with isolates
What each lesson covers:
- Async Basics and Futures: How Dart runs slow work without freezing: the event loop, Futures and their states, then and catchError, async and await, and waiting for several Futures with wait, any and timeout.
- Streams: How Streams deliver many values over time: creating them, listening with listen and await for, making your own with StreamController and async*, and changing them with map, where and friends.
- Isolates: How isolates run heavy work in the background with their own memory: Isolate.run for one job, Isolate.spawn with ReceivePort and SendPort for messages, and closing, killing and error listeners.
This chapter has 3 lessons. Plan on about 60 minutes, with 9 exercises to check what you learned. Every example is real Dart code: change it, run it again and compare the output, which is the fastest way to learn how each feature behaves.
Simple lesson: finish this chapter and your apps stay smooth while they download, read and calculate.
Lessons in this section
- Async Basics and Futures
How Dart runs slow work without freezing: the event loop, Futures and their states, then and catchError, async and await, and waiting for several Futures with wait, any and timeout.
- Explain sync vs async and the order of the event loop
- Create Futures and handle them with then, catchError and whenComplete
- Write async functions with await, try-catch, Future.wait and timeout
- Streams
How Streams deliver many values over time: creating them, listening with listen and await for, making your own with StreamController and async*, and changing them with map, where and friends.
- Create Streams and read them with listen and await for
- Write async* generators and use StreamController
- Transform Streams and tell single-subscription from broadcast
- Isolates
How isolates run heavy work in the background with their own memory: Isolate.run for one job, Isolate.spawn with ReceivePort and SendPort for messages, and closing, killing and error listeners.
- Explain isolates, memory isolation and how they differ from threads
- Run one heavy job with Isolate.run
- Send messages between isolates with spawn, ReceivePort and SendPort