Created
November 13, 2022 15:09
-
-
Save kamal-github/8cb3c9a31b293e70a37dde6bde491f49 to your computer and use it in GitHub Desktop.
Async await sample which explains the flow of execution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
Future<void> printOrderMessage() async { | |
print('printOrderMessage: Awaiting user order... fetches and returns Future<void>'); | |
var order = await fetchUserOrder(); | |
print('This and following execute only after Future is complete'); | |
print('Your order is: $order'); | |
} | |
Future<String> fetchUserOrder() { | |
print('fetchUserOrder: going to order with delay'); | |
return Future.delayed(const Duration(seconds: 3), () => 'Large Latte'); | |
} | |
void main() { | |
countSeconds(4); | |
print(printOrderMessage()); | |
print('exiting main()'); | |
} | |
void countSeconds(int s) { | |
for (var i = 1; i <= s; i++) { | |
// Registering into EventQueue of main isolate. | |
Future.delayed(Duration(seconds: i), () => print(i)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment