v137 · webassembly
JavaScript Promise Integration
JavaScript Promise Integration (JSPI) is an API that allows WebAssembly applications to integrate with JavaScript Promises.
concepts
-
JSPI
JSPI lets WebAssembly suspend on JS Promises directly. C/C++ ports get async semantics without manually pumping a state machine.
-
Wasm fetch()
The other JSPI shape: Wasm-as-Promise-producer. A wasm function calls a synchronous-looking
fetchhost import and the wrapped export hands back a Promise the JS side canawait. -
C-loop Unblocker
Two lanes for a ported C library that calls in a loop: synchronous shim (blocks the page) vs JSPI-suspended (UI keeps ticking). Live RAF pulse meter shows dropped frames in the legacy lane.
-
Stack-switching trace
Step or play through a JSPI call; the two stacks (JS + suspendable Wasm) render frame-by-frame with a chronological event log of
entered/suspended/resumedevents.
why it shipped
In order to support responsive applications written using WebAssembly it is necessary to provide functionality that allows WebAssembly programs to be suspended and resumed. The primary initial use case is to allow WebAssembly programs whose source relies on synchronous APIs to use asynchronous APIs that are increasingly common on the Web platform. The Promise integration proposal allows this to be done without changing the WebAssembly specification itself and without allowing JavaScript programs to be suspended (other than via existing async mechanisms).