Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate async function with async init #2484

Open
RReverser opened this issue Mar 8, 2021 · 0 comments
Open

Integrate async function with async init #2484

RReverser opened this issue Mar 8, 2021 · 0 comments

Comments

@RReverser
Copy link
Member

Motivation

#1905 added syntactic support for async + #[wasm_bindgen(start)], which means it's accepted by the macro, but it's not changing anything on the JS side.

It's leading to confusing race conditions where an async function marked with #[wasm_bindgen(start)] is doing some preparatory async work, and user expects it to be finished by the time await init(); is finished on --target web (or an equivalent or other targets).

On practice, however, wasm-bindgen generated init will only kickstart the async process, but not wait for its completion, so code like this fails in unexpected ways:

#[wasm_bindgen(start)]
pub async fn start() {
  // ...init some global state
}

#[wasm_bindgen]
pub fn foo() {
  // ...access the global state assuming it's ready
}
import init, { foo } from './pkg';

await init();
foo();

Proposed Solution

async functions marked with #[wasm_bindgen(start)] should create a promise that is awaited or returned from the wasm-bindgen-generated init function (or its equivalent on other targets).

Alternatives

As an alternative, async + #[wasm_bindgen(start)] could be disallowed to make it more clear that the function itself won't be awaited.

Additional Context

Add any other context or screenshots about the feature request here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant