라벨이 dart인 게시물 표시

What is Difference between async and async* in Dart?

## What is Difference between async and async* in Dart? async async keyword to a function that does some work that might take a long time. It returns the result wrapped in the Future. Future<int> doSomeLongTask() async { await Future.delayed(const Duration(seconds: 1)); return 42; } You can get that result by awaiting the Future:</int> main() async { int result = await doSomeLongTask(); print(result); // prints '42' after waiting 1 second } async* async* keyword to make a function that returns a bunch of future values one at a time. The results are wrapped in a Stream. Stream<int> countForOneMinute() async* { for (int i = 1; i <= 60; i++) { await Future.delayed(const Duration(seconds: 1)); yield i; } } You can use await to wait for each value emitted by the Stream.</int> main() async { await for (int i in countForOneMinute()) { print(i); // prints 1 to 60, one integer per second } }