In the process of creating a JS class, I encounter a situation where I need to call wasmFunc
in the constructor and store its output in this.val
. However, since wasmFunc
comes from a WebAssembly file, it is necessary to execute an async function called loadWasm
before wasmFunc
becomes callable. The code snippet below demonstrates my attempt:
// index.js
async loadWasm() {
// load wasm file
}
export class MyClass {
constructor() {
await loadWasm(); // This operation will not work
this.val = wasmFunc();
}
}
export const myObj = new MyClass();
My goal is for the exported file index.js
to offer a ready-to-use myObj
. Unfortunately, using await
within the constructor is prohibited.
Do you have any solutions or thoughts on how to address this challenge?