Skip to content

A rust library for working with js-sys functions as futures

Notifications You must be signed in to change notification settings

emily-curry/js-function-promisify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

js-function-promisify

crates.io 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

About

A rust library for working with js-sys functions as futures

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages