Await blocks

I think that await blocks would be useful It will await all further inside the block

this_fn_returns_a_future_1().await
this_fn_returns_a_future_2().await
this_fn_returns_a_future_3().await

But with await block:

await {
    this_fn_returns_a_future_1()
    this_fn_returns_a_future_2()
    this_fn_returns_a_future_3()
}

(NOT A CONTRIBUTION)

The semantics of this feature are unclear; do these all execute concurrently or just one after another?

If you want them to run concurrently, you should use the join macro provided by a library like futures, tokio, or async-std. If you are on nightly, such a macro is available in std as well: join in std::future - Rust

1 Like

Basically all futures get awaited inside of an await block

they run one after another

no, there's no point in adding this, this does not solve any problems

6 Likes

How about this code example:

let a = foo().await?;
let b = a.bar()?.baz().await;
let c = b.qux().await?;

If you turn it into an await block, how does the compiler know which expressions to await? If it automatically awaits everything that implements IntoFuture, the control flow becomes implicit, which is the opposite of what we tried to achieve with async/await.

6 Likes

Also, sometimes functions return Futures but you don't want to .await them. See for example tokio::spawn.

3 Likes

But you do explicitly put it in an await block and if it shouldn’t be awaited on the you don’t put it in an await block

.await is easy to write and very clear. This creates rightward drift, and loses local explicitness of the await points.

Indentation and braces don't even make the code more compact.

Because it's a block, it also complicates keeping results returned from functions inside the block (needs bindings declared earlier or some kind of super let).

Serial execution of async functions without data inter-dependencies seems like a wasted opportunity to run them concurrently.

Sorry, I can see only negatives of this proposal.

7 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.