I want to share how I verify the point: everything after await
keyword is sent to event queue.
When I first come across the code in the book below:
Future<void> main() async {
print('Before the future');
final value = await Future<int>.delayed(
Duration(seconds: 1),
() => 42,
);
print('Value: $value');
print('After the future');
}
It look like to me that the last two lines are synchronous also. The await
keyword just stop the program temporarily and run the Future
, then go back to run the last two lines.
Until the book tells me everything after the await
keyword is sent to the event queue. I just want to verify this point. Therefore, I re-write the code as follows:
Future<void> main() async {
print('Before the future');
Future<int> futureFunction() {
Future.microtask(
() => print("put microtask queue"),
);
return Future.value(42);
}
final value = await futureFunction();
print('Value: $value');
print('After the future');
}
The print output put microtask queue
exists before the last 2 print lines. Now, I am convinced the last two lines are also in event queue. This is because if the last two line is synchronous, they will have higher priority than the microtask queue. Then, the microtask print output should be at last.