Dart by Example: Yield*

  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. if (n >= 0) {
  9. await new Future.delayed(new Duration(milliseconds: 100));
  10. yield n;
  11. yield* numbersDownFrom(n - 1);
  12. }
  13. }
  14.  
  1. $ dart yield_each.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