dart-lang / dart-lang/language

await for ... and ... syntax

Open
#1,440 5 comments 0 reactions 0 assignees View on GitHub
feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

I'm not aware of any way to subscribe to two streams using `await for` in an `async*` block. There are stream combining tools made available in `rx_dart` but I believe there is a lot of benefit to making this available with the core async tools.

As it stands today, I am not aware of any way to combine these two streams in an `async*` block.

```dart
Stream deviceStatus() async* {
var status = BBQStatus.initial();

/// Temperature probe events
await for (var e in service.probeEvents()) {
status = status.copyWith(probes: e);
yield status;
}

print('This is not reachable until probeEvents stream closes');

/// Battery voltage events
await for (var e in service.batteryEvents()) {
status = status.copyWith(battery: e);
yield status;
}

print('This is not reachable until both streams close');
}
```

You can combine streams using solutions like rx_dart's `CombineLatestStream.combine2`, but having syntax to allow us to handle this would be really nice.

Here is a concept that is similar to syntax proposed by https://github.com/dart-lang/language/issues/1441

```dart
Stream deviceStatus() async* {
var status = BBQStatus.initial();

/// Temperature probe events and battery voltage events
await for (var e in service.probeEvents()) {
status = status.copyWith(probes: e);
yield status;
} and (var e in service.batteryEvents()) {
status = status.copyWith(battery: e);
yield status;
}

print('This is reachable when probeEvents and batteryEvents subscriptions complete');
}
```

Contributor guide

Open the contributing guide

Research direction

Start with the proposed await for ... and ... examples in this issue, then review the related language discussion at dart-lang/language#1441. Investigate the language-design implications for concurrent stream subscriptions, including completion and error behavior. Done means the proposal has an accepted design and corresponding language specification work identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.