Dart by Example: Async / Await
1. import 'dart:async';
3. const Duration delay = const Duration(milliseconds: 200);
5. // This function doesn't use async / await; just the
6. // standard Future API
7. Future loadLastName(String firstName) {
8. return new Future.delayed(delay).then((_) {
9. return firstName + 'son';
10. });
11. }
13. // Marking a function with 'async' will return a future
14. // that completes with the returned value.
15. // This function is equivalent to [loadLastName]
16. Future loadLastName2(String firstName) async {
17. await new Future.delayed(delay);
19. return firstName + 'son';
20. }
22. main() async {
23. // 'await' will suspend execution of the function until the
24. // future completes:
25. var gregsLastName = await loadLastName('greg');
26. var stevesLastName = await loadLastName2('steve');
28. print('greg $gregsLastName');
29. print('steve $stevesLastName');
30. }
1. $ dart async_await.dart
2. greg gregson
3. steve steveson
by @jryanio | source | license