rust-lang / futures-rs

Zero-cost asynchronous programming in Rust
https://rust-lang.github.io/futures-rs/
Apache License 2.0
5.34k stars 616 forks source link

block_on in wasm #2741

Open simonwarchol opened 1 year ago

simonwarchol commented 1 year ago

New to rust, so appreciate the help.

I'm trying to call block on an async function within rust and it's throwing errors, was wondering if this is possible.

Using futures = {version="0.3.28", features=["executor"]}

use futures::executor::block_on;
pub fn test() -> Vec<f32> {
    block_on(test_diff())
}

pub async fn test_diff() -> Vec<f32> {
    let result = async_func.await;
    result
}

but getting a RuntimeError: unreachable error.

Any guidance would be greatly appreciated.

notgull commented 1 year ago

Generally, in WASM, you never want to actively "block". It's all single threaded, so "blocking" would make everything unresponsive. This is why mutexes and other thread blocking primitives don't work on WASM.

Instead, you want to spawn your future.