The current specification delays running the initial part of an "async" function until a later microtask.
However, that makes async functions unusable when implementing functions that require action to be taken immediately.
One such example is the "first" getter on the Stream class. It should start listening to the stream immediately, and then complete its returned future with the first event it receives.
Implementing it using an async function seems like it could be:
Future<T> get first async {
await for (T element in this) return element;
throw "no such element";
}
However, that function doesn't listen on the stream immediately, and if the stream is a broadcast stream, the delay may cause it to miss events sent after the call to first.
This problem prevents implementing most Stream functions using async code.
I recommend letting the initial part of an async function be run immediately when it's called instead of being delayed.
Haha, a bit of an oldie, but I'd love to see this fixed up! I couldn't figure out why this works with async* functions:
new StreamController().stream.asBroadcastStream();
But this does not:
new StreamController.broadcast().stream;
@brianegan I thought this change is already active in recent Dart 2 versions.
@zoechi Thanks! I'll do some testing with my lib + Dart2!
Might need --preview-dart-2 but I'm pretty sure I saw that work was done here.
This has been specified and implemented. In Dart 2, this will be the behavior.
For those interested -- this only applies to async functions, but not to async* functions, which is a detail I missed. This issue tracks async* functions: https://github.com/dart-lang/sdk/issues/33818