demo · v137
JSPI — WebAssembly suspends on JS Promises
JSPI lets a C / C++ port call a synchronous-looking host function that actually awaits a JS Promise — the Wasm stack suspends. Below: a tiny Wasm module imports an async sleep and treats it like a blocking call. The button runs it; observe how the JS side stays responsive.
Heads upJSPI requires the
WebAssembly.promising wrapper. If your build is older, the probe will say so and the button will use a non-JSPI fallback that demonstrates the difference.probing…
no run yet
the code
// Async import to Wasm:
const imports = {
env: {
sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
},
};
const { instance } = await WebAssembly.instantiate(bytes, imports);
// Wrap the export so it can suspend on Promises:
const runAsync = WebAssembly.promising(instance.exports.run);
await runAsync(3, 1000); // wasm calls sleep() 3× as if it were synchronous
see also
- JavaScript Promise Integration — feature index
- ChromeStatus entry