demo · v137
Wasm fetch()
The second JSPI shape: Wasm-as-Promise-producer. A C/C++/Rust port calls fetch() through a synchronous-looking host import; the JS caller waits on the Promise that the wrapped export returns. This is the pattern that lets unmodified libcurl-style code work on the web.
Heads up · origin trialRequires Chrome 137+ with JSPI shipped, or the flag
chrome://flags/#enable-experimental-webassembly-jspi. The probe below reports your exact state.probing…
JS side log (Promise consumer)
no run yet
"wasm" side log (suspendable producer)
no run yet
status
—
bytes
—
total wall time
—
the code
// Hypothetical C ported via Emscripten:
//
// int fetch_get(const char* url, char* out, int max);
// // declared as if it blocks. Under the hood it calls
// // js_fetch() which returns a Promise.
//
// On the JS side, that host import resolves with the body.
const imports = {
env: {
js_fetch: async (urlPtr, urlLen) => {
const url = readString(memory, urlPtr, urlLen);
const r = await fetch(url);
return r.text(); // imports can return Promises with JSPI
},
},
};
// Wrap the export. The Promise yields when the import suspends.
const fetchGet = WebAssembly.promising(instance.exports.fetch_get);
const body = await fetchGet(urlPtr, urlLen); // wasm "blocks" on fetch
see also
- JavaScript Promise Integration — feature index
- JSPI sleep demo
- V8 blog
- ChromeStatus entry