-
Notifications
You must be signed in to change notification settings - Fork 213
Prefix await
is cumbersome to work with.
#25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Idea: Special-case the /// Changes the contents of [file] to uppercase.
/// Completes when the file is written back to disk.
Future<void> contentsToUpperCase(File file) async =>
file.readAsString()
|> await
|. toUpperCase()
|> file.writeAsString
|> await; |
Is this still being considered? It looks like this would be a very small non-breaking syntactic change, but would make working with nested futures much more pleasant. For example, I'm currently working with records that form a graph in a database so that each record has a final someChild = (await (await (await db.get(someId)).children).first.children).first; With the proposed postfix await this would become much less cumbersome: final someChild = db.get(someId).await.children.await.first.children.await.first; (Of course something even shorter than |
In these cases I will simply use final someChild =
await db.get(someId)
.then((o) => o.children)
.then((o) => o.first.children)
.then((o) => o.first)
This would be even better if we could have something like #691. |
To be honest I think expr
..bar()
..baz() await
..qux(); and expr
..bar()
..baz().await
..qux(); are kinda confusing because I'm just so used to seeing Maybe I'm just boring, but what about this? expr
..bar()
..await baz()
..qux(); The only thing i don't like about it is that |
Putting the The It doesn't really work as an incremental change, though. To do this, it would mean that |
For reference, Rust decided to use postfix syntax for |
@jsanl wrote:
Totally in favor of this syntax! |
Using a prefix operator inline in a selector chain is an enticing idea. |
Is this being considered? Are there any developments on the topic? |
@lrhn |
any updates ? whats the issue with thanks |
any updates? needed async in my |
No updates. We've been focused on larger higher priority issues (metaprogramming, primary constructors, static member shorthands, etc.). |
Maybe this extension method can solve the problem for cascade? (Not sure) extension <T> on Future<T> {
Future<T> get wait async => await this;
} Example: class A {
int x = 0;
Future<int> myMethod() async => 42;
}
void main() async {
var a=A()
..myMethod().wait
..x=1;
print(a.x);
} My guess is that it doesn't really work as intended, but creates so good an illusion that it can pass for a real thing :-) It also shows that if |
That's correct. The extension method here doesn't actually do anything. The caller would still need to await the result. |
I would have made extension UnawaitedFuture on Future<Object?> {
void get unawaited {}
} too. (Or should it be |
In an asynchronous Dart function, the
await expr
expression allows blocking and waiting for a future result ofexpr
to complete.This is highly convenient compared to using
Future.then
, but grammatically it's still cumbersome because await is a prefix operator with lower precedence than selectors.Example:
If you need to await an intermediate result of a longer computation chain, you need to add parentheses and go back and write the await far distanced from the operation that created the future.
If you have a cascade like:
and
baz
is (or becomes) asynchronous and you want to await it before continuing, then you have to rewrite it to something like:A solution (proposal!) is be to allow
.await
as a suffix operator instead of onlyawait
as a prefix operator:This is still only allowed inside asynchronous functions where
await
is a reserved word, so it would not be ambiguous.It's a special syntactic form, not a named member access. An
await
is a special kind of selector.You can do
expr?.await
,expr..await
andexpr?..await
too. It will await (of not null), then throw away the result if it's a cascade (or follow it with more selectors,expr..await.selectors
, and still evaluate to the original future ... but why?)Since this is new syntax, I'd require the operand to have a type which implies a future type (implement
Future
, beFutureOr
, or nullable either of those, ordynamic
for being dynamic). No awaiting a non-future. (You can always upcast yourint
toFutureOr<int>
and await that, though. At least you have to be explicit about it.)See also #1216, #2762, dart-lang/sdk#25986, dart-lang/sdk#23000.
(Edit, 5+ years later: Definitely with a
.
in front, notfoo() await
. Grammar is precious,.await
is simple, updated to suggest only that.)The text was updated successfully, but these errors were encountered: