Skip to content

Instantly share code, notes, and snippets.

@kamal-github
Created November 13, 2022 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamal-github/8cb3c9a31b293e70a37dde6bde491f49 to your computer and use it in GitHub Desktop.
Save kamal-github/8cb3c9a31b293e70a37dde6bde491f49 to your computer and use it in GitHub Desktop.
Async await sample which explains the flow of execution.
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