Async, await, and wasm32/ajax

Does the newly released async/await simplify writing AJAX handlers in Rust/Wasm32?

If so, is there recommended reading / a good blog post?

The interface through web_sys still returns a Promise, and I think that's by design to keep web_sys unopinionated and basically a thin interface to the native API

Here's one way of fetching() (given a Window):

fetch_with_request(&self, input: &Request) -> Promise

Getting from a Promise to a Future is a piece of cake with wasm_bindgen_futures ... previously it was behind a futures_0_3 feature flag but I think that's now been made default? (not sure)

In any case it's just JsFuture::from(promise)

For example, something like this should work:

async {
    let promise:Promise = window.fetch_with_request(&req);
    let resp_value = JsFuture::from(promise).await?;
    let resp: Response = resp_value.dyn_into()?;

Previously you needed to use wasm_bindgen_futures::futures_0_3::JsFuture;

1 Like

That’s correct you no longer need the feature.

1 Like