I'm trying to load a WASM library from another node js application (Both using webpack). On the WASM library, I have following code to export the functionality.
export default import("./pkg")
.then(s => {
let result = s.initialize_sync();
console.log("result = ", result);
return s;
}).catch(error => {
console.error(error);
throw error;
});
And on the application, I have the following.
let s = await import("s-wasm");
console.log("2 s = ", s);
I'm getting the following log lines when running the application.
2 s = { default: {} }
result = initialized
What I am expecting:
result = initialized
2 s = { default: {} }
I want the 2 s = line to be printed after WASM is loaded. but it is called before the WASM is loaded. How do I achieve this ? What am I doing wrong here ?
I would suggest you either write your module as
export * from "./pkg";
import { initialize_sync } from "./pkg";
console.log("result = ", initialize_sync());
or (if you really have to) with top-level await:
const s = await import("./pkg")
console.log("result = ", s.initialize_sync());
export default s;
so that you are not default-exporting a promise for s.
If you don't do that, your application code would need to be
let s = await (await import("s-wasm")).default;
console.log("2 s = ", s);