Async with both wasm and main

So I am trying to build a wasm module that have functions compatile with the main function, i.e. compilable with both lib.rs and main.rs.

I need to download a font, and for wasm, I guess putting async into it is the only way????
However, doing so would need to make the main function async, but Tokio is not yet supported with wasm.

So what are my option, including somekind of dependency config in cargo.toml is fine.
Thank you.
(Also, that do make me wonder if can I self-impliment a Future with polling and while loop to check polling ready, so that I do not neet to use async anymore)

Ok, I got it
The solution is to use the crate async-std instead of tokio:

async-std = { version = "1.12", features = ["attributes", "tokio1"] }

In the main function of main.rs, use use async_std::task::block_on on any function that requires async.

Calling block_on on a future that internally uses a javascript promise doesn't work for wasm. The javascript promise won't be fulfilled until you yield the javascript event loop, which block_on won't do by definition. For working with javascript promises you should use the wasm_bindgen_futures crate.

2 Likes

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.