wasm_promise_callbacks

0.0.2 • Public • Published

This will let you call js promise-functions from C wasm, with callbacks instead of asyncify/etc.

Essentially, you pick a host implementation, and a wasm-implementation for your language, and they will allow callbacks, and it's able to respond.

host

JS (node/browser/etc)

import { wrapPromise, callPromise } from "wasm_promise_callbacks";

const imports = {
  env: {
    // here is a promise-returning function that the wasm will get the output of
    example_host_func: wrapPromise((messageP) => new Promise((resolve) => setTimeout(() => resolve('nice!'), 1000)))
  },
};

const wasmBytes = "...";
const mod = await WebAssembly.compile(wasmBytes);
const instance = await WebAssembly.instantiate(mod, imports);

// wait for getting the response back
const response = await callPromise(instance.exports, instance.exports.test());

wasm

C

For a C example, that can perform http-requests and process it, see:

I am faking http, but you can see the result is the output of mycallback

Readme

Keywords

Package Sidebar

Install

npm i wasm_promise_callbacks

Weekly Downloads

3

Version

0.0.2

License

MIT

Unpacked Size

34.7 kB

Total Files

7

Last publish

Collaborators

  • konsumer