(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:
async_std::task::block_on(....)
now, however, suppose I have done:
let join_handle = async_std::task::spawn(...);
how do I block on this join_handle
?
.await
?
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
async_std::task::block_on(join_handle);
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.
Example:
let join_handle = async_std::task::spawn(async { 151 });
let x = async_std::task::block_on(join_handle);
assert_eq!(x, 151);
2 Likes
system
Closed
March 6, 2022, 6:53am
7
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.