Disambiguate return type of `async` block

I currently have code like this:

task::spawn(async move {
    loop {
        if let Err(err) = some_fallible_future.await {
            return Err::<Infallible, _>(err);

Is it currently possible on stable to disambiguate the return type of the async block? I'd like to be able to write some_fallible_future.await?, but ? doesn't work as written. The only thing I've managed to do is add an Ok::<_, ErrorType>(()) after the loop, which is, of course, unreachable (any correctly indicated as such).

No, you can do it for closures, but I don't think the syntax exists for async blocks. You can define an async fn, on which you would have to define the return type?

1 Like

Thanks. I knew I could define an async fn, but was hoping to avoid it for a simple case like this. I guess I'll have to wait for type ascription or async closures to land for it to be as clean as I'd like.

1 Like

For the particular case of task::spawn, you can do something like this (works in both tokio and async-std):

let _: JoinHandle<Result<_, ErrorType>> = task::spawn(async move { ... });

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.