rustfuturewebassemblybevy

Block execution till a Rust future is resolved on WASM without wasm bindgen


I am using a library (Macroquad) that has an async function with following signature:

async fn load_file(path: &str) -> Vec<u8>

This loads the file and returns the raw bytes.

Is there a way to call this function from a sync context and block till the future is resolved and get the bytes on wasm32-unknown-unknown, without wasm bindgen or other js glue code? I need to call it from a bevy system.


Solution

  • This is impossible, or close enough; you cannot execute any future that depends on async web APIs without letting the browser/JS event loop run by returning to it.

    However, there may be better solutions. It seems strange to me that you are using Macroquad and Bevy in the same application. I would suggest that you try using Bevy's asset loading instead, which is designed to work asynchronously and be used by Bevy systems.