A final proposal for await syntax

You can create a defer in user-space. I explored that in this thread:

let x = async {
    let foo = Defer::new(async_foo()).await;
    let bar = Defer::new(async_bar()).await;
    qux(foo.await?.baz(), bar.await)
};

Bit heavy-weight, but you can use it today, no need to wait for changes to the language.

With a macro it becomes more manageable:

let x = async {
    let foo = defer!(async_foo());
    let bar = defer!(async_bar());
    qux(foo.await?.baz(), bar.await)
};

Theoretically you could even create a proc macro which implements your syntax:

let x = defer! { qux(async_foo().defer?.baz(), async_bar().defer) };

I also need to point out that there also exists join and try_join functions which allow you to run Futures in parallel:

let x = async {
    let (foo, bar) = join(
        async_foo(),
        async_bar(),
    ).await;

    qux(foo, bar)
};

They’re not as flexible as defer, but they’re faster, and in my opinion easier to understand.

2 Likes