Given the following (simplified) Rust code:
#[wasm_bindgen(js_name = MyClass)]
pub struct MyClassJs {
my_struct: MyStruct,
}
#[wasm_bindgen(js_class = MyClass)]
impl MyClassJs {
#[wasm_bindgen(constructor)]
pub async fn new() -> Result<MyClassJs, JsValue> {
// ...
}
}
Compiled using wasm-pack
, and this recommendation from the wasm-bindgen book:
// Note that a dynamic `import` statement here is required due to
// webpack/webpack#6615, but in theory `import { greet } from './pkg';`
// will work here one day as well!
const rust = import('./pkg');
rust
.then(m => m.greet('World!'))
.catch(console.error);
The previous statement makes me believe that doing any rust-wasm related calls are illegal until the Promise has resolved.
Which poses the question: which of these statements is true?
- Is it legal to call
new MyClassJs()
at any time (even before the Promise is resolved)? - Is it legal to call
new MyClassJs()
only after the Promise is resolved (but anywhere, including outside thethen
callback? - Is it legal to call
new MyClassJs()
only inside thethen
callback?
I have tried finding an answer using these resources, but have found them to not contain the answer to my question:
- https://rustwasm.github.io/docs/book/
- https://rustwasm.github.io/docs/wasm-pack/
- https://rustwasm.github.io/wasm-bindgen/
Edit: I am currently on webpack 4.