dart-lang / dart-lang/language

Add "asyncOr", similar to "async", for FutureOr

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

Description

A common UI problem is to want to make potentially asynchronous code synchronous if possible, to avoid flickers where progress indicators show only for a frame or two.

While there are solutions to this problem, the issue is, those solutions are strictly incompatible with `async`/`await` – decreasing the code readability quite a bit.

## Proposal

To solve the problem of async/await being incompatible with code-paths that are potentially synchronous, we could introduce an `async` variant that returns a `FutureOr`

Meaning we could write:

```dart
FutureOr multiplyByTwo() keyword {
FutureOr futureOr;
T value = await futureOr;
return value * 2;
}
```

With this variant, `await` _may_ execute synchronously (when it currently would always be async). Meaning that the previous snippet would be equivalent to doing:

```dart
FutureOr multiplyByTwo() {
FutureOr futureOr;
if (futureOr is Futureint>)
return futureOr.then((value) => value * 2);
return value * 2;
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the proposal and its examples, then compare the requested FutureOr behavior with the existing async/await language rules. Done means the semantics and compatibility implications of an asyncOr variant are resolved clearly enough for the language team to evaluate; this issue names no implementation files or tests.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.