Reputation: 71
I know that wasm doesn't have blocking so how would I do the equivalent. Or is there a different tool to use? I'm new to this so it could also be that my approach is completely off. I'm using wasm-pack with a web target to compile.
fn fetch_definitions(def_dir: &str, spelling: &str) -> Result<WordDefs, Error> {
let path = format!("{def_dir}/{spelling}_definitions.json");
let r = wasm_bindgen_futures::spawn_local(async move {
let response: WordDefs = gloo_net::http::Request::get(path.as_str())
.send()
.await
.unwrap()
.json()
.await
.unwrap();
});
// what I would normally do to get a value from a future
// let value = executor::block_on(r);
Err(Error::CouldNotLookUpWord)
}
As I know I have to use wasm_bindgen_futures for the async block and spawn_local has output=(), I can figure out how to get around this.
Upvotes: 6
Views: 1439
Reputation: 1116
I guess you are using Yew with wasm_bindgen_futures ?
use wasm_bindgen_futures::spawn_local;
the solution here is for Yew == 0.21.0
pub enum Msg
{
ResultFromSpawn ( WordDefs ), // WordDefs is the result from your spwan_local
//... your other Msgs,
}
fn fetch_definitions( ctx: &Context<Self> ... your other params .. ) -> Result<WordDefs, Error>
{
//.. your stuffs
let link = ctx.link().clone(); // this is where you get link from ctx
// I removed let r = .. from your original code
wasm_bindgen_futures::spawn_local( async move
{
let response: WordDefs = gloo_net::http::Request::get(path.as_str())
.send()
.await
.unwrap()
.json()
.await
.unwrap();
link.sendMessage( Msg::ResultFromSpawn( response ) ); // use "link" to pass data to Msg
});
//... your stuffs
}
then you can process Msg as usual in update function as below;
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool
{
match msg
{
Msg::ResultFromSpawn( response ) =>
{
// process your response ( type of WordDefs ) here
}
// other Msgs
}
}
Upvotes: 1
Reputation: 206
I think we can use async-std crate with "unstable" feature like:
let r = async_std::task::spawn_local(async move {
let response: WordDefs = gloo_net::http::Request::get(path.as_str())
.send()
.await
.unwrap()
.json()
.await
.unwrap();
});
Upvotes: 0
Reputation: 5827
This isn't possible due to the single threaded nature of browsers, see this issue for more info.
Upvotes: 0