Dart by Example: Await For

  1. import 'dart:async';
  2. main() async {
  3. await for (int i in numbersDownFrom(5)) {
  4. print('$i bottles of beer');
  5. }
  6. }
  7. Stream numbersDownFrom(int n) async* {
  8. while (n >= 0) {
  9. await new Future.delayed(new Duration(milliseconds: 100));
  10. yield n--;
  11. }
  12. }
  13.  
  1. $ dart await_for.dart
  2. 5 bottles of beer
  3. 4 bottles of beer
  4. 3 bottles of beer
  5. 2 bottles of beer
  6. 1 bottles of beer
  7. 0 bottles of beer

by @jryanio | source | license