#js #async

js-function-promisify

A library for working with js-sys functions as futures

2 releases

0.2.1 May 11, 2021
0.2.0 May 11, 2021

#1026 in WebAssembly


Used in fut_rwlock

MIT license

28KB
540 lines

js-function-promisify

docs

This package provides utilities for working with js_sys::Function callbacks in async rust the same way you might in javascript. For example, to wait on a timeout to complete in javascript, one might write:

const promise = new Promise((resolve) => {
  window.setTimeout(() => { resolve('Hello future!'); }, 500);
});
const result = await promise;
result === 'Hello future!'; // true

To accomplish the same thing with js-function-promisify:

let future = Callback::new(|| Ok("Hello future!".into()));
web_sys::window()
  .unwrap()
  .set_timeout_with_callback_and_timeout_and_arguments_0(
    future.as_function().as_ref(),
    500)
  .unwrap();
let result = future.await; // result: Result<JsValue, JsValue>
assert_eq!(result.unwrap().as_string().unwrap(), "Hello future!"); // 🦀

Usage

TODO: Document common ways to use Callback. TODO: Document gotchas, primarily use of Closure::once and deallocation on first call.

Contributing

Contributions are welcome! Feel free to open an issue if you find a bug or have a feature request. Additionally, feedback on the public API is more than welcome, as this is the first library I've published and I'm still getting a feel for what's idiomatic in rust.

Testing

Using wasm-pack is the easiest way to run the tests in this repository. Those folks have some great documentation on it here and here, but as an example one may run the following to run all tests in the library:

wasm-pack test --headless --firefox

Dependencies

~0.9–1.6MB
~30K SLoC