Blocking on an async_std::task::spawn(_)

(this is almost certainly a dumb question; I just can't find the answer on JoinHandle in async_std::task - Rust )

Normally, if I want to block on something, I can do:


now, however, suppose I have done:

let join_handle = async_std::task::spawn(...);

how do I block on this join_handle ?

Because, I think JoinHandle implements the Future trait, hence it should be awaitable.

This is my fault for not mentioning it original question:

we are in a regular, non-async function, so we can't use .await

Well, if you don't mind using a nightly-only trait - IntoFuture, which is implemented by JoinHandle. So, you can call, async_std::task::block_on(join_handle.into_future());.

On stable, I can do


and it compiles and appears to do the right thing. However, I have no idea what is going on.

JoinHandle implements Future, so can be passed to block_on. It resolves to the value returned by the task that was spawned.


let join_handle = async_std::task::spawn(async { 151 });
let x = async_std::task::block_on(join_handle);
assert_eq!(x, 151);

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.