#futures #bindgen #gap #promises #bridges #conversions

wasm-bindgen-futures

Bridging the gap between Rust Futures and JavaScript Promises

53 releases

0.4.18 Sep 9, 2020
0.4.17 Jul 28, 2020
0.4.14 Jun 29, 2020
0.4.10 Mar 26, 2020
0.3.5 Nov 12, 2018

#89 in WebAssembly

Download history 54025/week @ 2020-06-30 51469/week @ 2020-07-07 54938/week @ 2020-07-14 52106/week @ 2020-07-21 55836/week @ 2020-07-28 52605/week @ 2020-08-04 57577/week @ 2020-08-11 53393/week @ 2020-08-18 54688/week @ 2020-08-25 51096/week @ 2020-09-01 57899/week @ 2020-09-08 56310/week @ 2020-09-15 58066/week @ 2020-09-22 59807/week @ 2020-09-29 61731/week @ 2020-10-06 54265/week @ 2020-10-13

231,893 downloads per month
Used in 1,669 crates (110 directly)

MIT/Apache

2MB
36K SLoC

wasm-bindgen-futures

API Documention

This crate bridges the gap between a Rust Future and a JavaScript Promise. It provides two conversions:

  1. From a JavaScript Promise into a Rust Future.
  2. From a Rust Future into a JavaScript Promise.

See the API documentation for more info.


lib.rs:

Converting between JavaScript Promises to Rust Futures.

This crate provides a bridge for working with JavaScript Promise types as a Rust Future, and similarly contains utilities to turn a rust Future into a JavaScript Promise. This can be useful when working with asynchronous or otherwise blocking work in Rust (wasm), and provides the ability to interoperate with JavaScript events and JavaScript I/O primitives.

There are three main interfaces in this crate currently:

  1. JsFuture

    A type that is constructed with a Promise and can then be used as a Future<Output = Result<JsValue, JsValue>>. This Rust future will resolve or reject with the value coming out of the Promise.

  2. future_to_promise

    Converts a Rust Future<Output = Result<JsValue, JsValue>> into a JavaScript Promise. The future's result will translate to either a resolved or rejected Promise in JavaScript.

  3. spawn_local

    Spawns a Future<Output = ()> on the current thread. This is the best way to run a Future in Rust without sending it to JavaScript.

These three items should provide enough of a bridge to interoperate the two systems and make sure that Rust/JavaScript can work together with asynchronous and I/O work.

Dependencies

~0.8–1.3MB
~29K SLoC