-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
This issue was originally filed by @seaneagan
In the same way that programming with Futures could benefit from language support (issue #104), programming with Streams could as well. Streams are essentially async Iterables, so an async version of the "for" statement could work for Streams. It probably makes sense to call it "on" which is already a contextual keyword elsewhere. As an example the implementation of the Iterable-based methods on Stream could look identical to how they are in Iterable except replacing "on" with "for":
async bool any(bool f(E e)) {
on(var e in this) {
if(f(e)) return true;
}
return false;
}
notes:
* the "on" statement would trigger a "subscribe" to the Stream passing onData, onDone, and onError resulting in a StreamSubscription
- as with await, the enclosing function would implicitly return a Future
- as with await, errors within the construct would throw the unwrapped error, not the AsyncError wrapper
- any "break" or "return" in the "on" statement would trigger an "unsubscribe" on the StreamSubscription
- nested "await"s and "on"s could be delimited by "pause()" and "resume()" on the StreamSubscription, and since that's only a best effort, any events which occur before the nested items complete could be buffered for use in later iterations of the "on" statement.
Initial mailing list discussion:
Activity
anders-sandholm commentedon Nov 29, 2012
cc @floitschG.
Added Area-Language, Triaged labels.
DartBot commentedon Jan 2, 2013
This comment was originally written by jjinux...@google.com
Thank you for the excellent issue description!
DartBot commentedon Feb 5, 2014
This comment was originally written by @seaneagan
Related idea... async generators (issue #36 is sync generators). Functions which use (
await
ORon
(see above)) ANDyield
would return Streams.Examples:
Stream everyOtherEvent(Stream stream) {
int i = 0;
on(var e in Stream) if(i.isEven) yield e;
}
/// Think... "Connection error... retrying in <x seconds>"
Stream<Duration> retry(action, Duration initial, {int max}) {
var duration = initial;
var tries = 0;
while(max == null || tries++ <= max) {
if(action()) return;
yield await new Future.delayed(duration);
duration *= 2;
}
throw '...';
}
gbracha commentedon Aug 22, 2014
We are looking at adding such support as part of the async-await proposal.
Set owner to @gbracha.
Added Accepted label.
gbracha commentedon Jan 3, 2015
The Dart spec now includes async* methods and await for loops. Not fully implemented yet, but in progress. I'm closing this bug as "Done" from the language perspective.
Added Done label.