Rust vs JS async/await

I'm confused by interaction of Rust async and JS async when dealing with Chrome/wasm32.

Is it correct to say:

  1. Rust asyncs are "lazy" in that we have to await on them for them to do work.

  2. JS asyncs are "eager" in that from the moment they are created, they start doing work, even if we do not await on them.

Yes, a Rust future doesn't do anything until it's polled and a JS Promise is scheduled basically immediately on creation.

1 Like

More precisely, a JS Promise is not scheduled at all; it is only a handle to the output of something that will or won't complete on its own.

1 Like

Does this imply that anything that crosses the JS <-> Rust border, in either direction, is eager?

For JS Promise -> Rust async: it's eager because the JS Promise is eager

For Rust async -> JS promise: We create + schedule JS promise. This polls the Rust async. Thus eager.

Is this logic correct ?

Nothing makes Rust Futures eager, you always have to poll them to make progress. If you pass a future to something that polls it when a JS task completes you haven't made the Rust Future eager, something else is just doing the polling.

1 Like

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.