#futures #bindgen #gap #future #promises #bridges

wasm-bindgen-futures

Bridging the gap between Rust Futures and JavaScript Promises

63 releases

0.4.28 Sep 15, 2021
0.4.26 Aug 19, 2021
0.4.24 May 10, 2021
0.4.23 Mar 29, 2021
0.3.5 Nov 12, 2018

#553 in WebAssembly

Download history 154642/week @ 2021-08-15 159637/week @ 2021-08-22 149598/week @ 2021-08-29 140236/week @ 2021-09-05 164502/week @ 2021-09-12 151981/week @ 2021-09-19 149372/week @ 2021-09-26 166749/week @ 2021-10-03 189939/week @ 2021-10-10 204645/week @ 2021-10-17 188499/week @ 2021-10-24 184180/week @ 2021-10-31 172773/week @ 2021-11-07 162509/week @ 2021-11-14 148345/week @ 2021-11-21 160121/week @ 2021-11-28

663,509 downloads per month
Used in 4,224 crates (236 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.

Additionally under the feature flag futures-core-03-stream there is experimental support for AsyncIterator to Stream conversion.

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.7–1.2MB
~26K SLoC