Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Created September 3, 2020 15:46
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 graphicbeacon/e9e9db9c339c29d63a32f55b44d6a38d to your computer and use it in GitHub Desktop.
Save graphicbeacon/e9e9db9c339c29d63a32f55b44d6a38d to your computer and use it in GitHub Desktop.
Async Generators example
import 'dart:async';
void main() {
// asynchronousNaturalsTo(10).listen(print);
final controller = asyncNaturalsTo(10);
controller.stream.listen((n) => print('YEss $n'),
onDone: () {
controller.close();
});
}
Stream<int> asynchronousNaturalsTo(int n) async* {
int k = 0;
while (k < n) yield k++;
}
StreamController<int> asyncNaturalsTo(int n) {
final controller = StreamController<int>();
int k = 0;
while (k < n) {
controller.add(k);
k++;
}
return controller;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment