WebAssembly JavaScript Promise Integration (JSPI) enters origin trial

JavaScript Promise Integration (JSPI) is an API that allows synchronous sequential code compiled to WebAssembly to access asynchronous Web APIs. Many Web APIs are crafted in terms of JavaScript promises: instead of immediately performing the requested operation, they return a promise to do so. When the action is finally performed, the browser's task runner invokes any callbacks with the promise. JSPI hooks into this architecture to allow a WebAssembly application to be suspended when the promise is returned, and to be resumed when the promise is resolved.

From Chrome 123 until Chrome 128, JSPI is available as an origin trial. Learn more about JSPI in the blog post Introducing the WebAssembly JavaScript Promise Integration API or from the specification draft.

To get a better understanding of how to use this feature, consider the following excerpt of a C program that calculates the Fibonacci sequence by outsourcing the addition to a JavaScript function.

// This is C code.
long promiseFib(long x) {
  if (x == 0)
    return 0;
  if (x == 1)
    return 1;
  // This is where the C code calls asynchronous JavaScript.
  return promiseAdd(promiseFib(x - 1), promiseFib(x - 2));

// Addition artificially wrapped in a Promise.
EM_ASYNC_JS(long, promiseAdd, (long x, long y), {
  // This is asynchronous JavaScript code.
  return Promise.resolve(x+y);

To compile this C program to WebAssembly, use the following command, which requires the Emscripten SDK.

emcc -O3 badfib.c -o index.html -s ASYNCIFY=2

You can feature-detect JSPI support from JavaScript as follows:

if ('Suspender' in WebAssembly) {
  // JSPI is supported.

A demo of JSPI from the previous code sample is available on Glitch and also embedded in the following. You can see the Emscripten-generated source code if you want, but the actually interesting code is the original C program (clicking this link will download the code).

To try out JSPI with real users of your application, sign up for the origin trial. Read Get started with origin trials if you need instructions. The JSPI team is looking forward to receiving your origin trial feedback (see Step 5 in the instructions) so the feature fits your needs when it ships!