-
Notifications
You must be signed in to change notification settings - Fork 205
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
Prefix await
is cumbersome to work with.
#25
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 |
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 possible improvement would be to allow
await
as a suffix operator instead of a prefix operator:or maybe with a dot in front:
This is still only allowed inside asynchronous functions where
await
is a reserved word, so it would not be ambiguous.Other alternatives could be a suffix operator (like
!
, although that's probably high-priced syntactic real-estate, but if it is a general heuristic coercion operator, it could coerce fromFuture<T>
toT
too) or something else that allows bindingawait
better in compound expressions.The text was updated successfully, but these errors were encountered: