Code
webassembly
// Fetch and instantiate a WebAssembly module
const response = await fetch("module.wasm");
const bytes = await response.arrayBuffer();
// Async instantiation with imports
const { instance } = await WebAssembly.instantiate(bytes, {
env: {
log: (n) => console.log("wasm says:", n),
},
});
console.log(instance.exports.add(2, 3));
// Streaming instantiation (compiles while downloading)
const { instance: inst2 } = await WebAssembly.instantiateStreaming(
fetch("module.wasm"),
{ env: { log: console.log } }
);