WebAssembly.promising()

The WebAssembly.promising() static method is used to wrap an exported Wasm function that relies on an asynchronous operation (that is, an imported suspending function created via the WebAssembly.Suspending() constructor), turning it into a Promise.

See WebAssembly.Suspending for an explanation of how this functionality works.

Syntax

js
WebAssembly.promising(function)

Parameters

function

A reference to an exported Wasm function, typically available on the exports object available in the fulfilled value of a method such as WebAssembly.instantiateStreaming().

Return value

A function that wraps the original function passed into the promising() call, and can itself be called. The wrapper function takes the same arguments as the wrapped function, and returns a promise that fulfills with the wrapped function's results.

Exceptions

TypeError

The referenced function is not callable.

Examples

Basic usage

js
WebAssembly.instantiateStreaming(fetch("module.wasm"), { importObj }).then(
  (result) => {
    const fromWasm = WebAssembly.promising(
      result.instance.exports.exportedFunc,
    );
    fromWasm().then((result) => {
      // ...
    });
  },
);

See WebAssembly.Suspending for a full working example.

Specifications

Specification
WebAssembly JavaScript Interface: Promise Integration
# dom-webassembly-promising

Browser compatibility

See also