dart-lang / language

Design of the Dart language
Other
2.67k stars 205 forks source link

The `async*` function `yield` should work well with `await for`. #121

Open lrhn opened 5 years ago

lrhn commented 5 years ago

The async* function yield is specified to wait until the event has been delivered, giving the receiver time to pause or cancel in (immediate) response to receiving the event.

Implementations do not wait for the receiver. This makes it hard and inefficient to write async* functions that do not do too much work. The implementations should follow the specification and make the user experience better.

Associated feature specification has more details.

janosroden commented 5 years ago

Hi,

Guys, do you have any estimation regarding this issue? I faced with the issue while I used this bloc library and I wasn't able to control when the state machine changes its state. My workaround is to not use yield* :(

Let me drop in an example in pure dart:

import 'dart:async';

int latestValue = 0;

Stream<int> func1() async* {
  yield* func2();
}

Stream<int> func2() async* {
  yield 1;
  print(latestValue); // prints "0", but "1" expected

  yield 2;
  print(latestValue); // "1"
}

void main() {
  func1().listen((i) => latestValue = i);
}