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

wasm-bindgen-futures

Bridging the gap between Rust Futures and JavaScript Promises

58 releases

0.4.23 Mar 29, 2021
0.4.21 Feb 26, 2021
0.4.19 Nov 30, 2020
0.4.17 Jul 28, 2020
0.3.5 Nov 12, 2018

#371 in WebAssembly

Download history 73606/week @ 2021-01-14 82760/week @ 2021-01-21 85065/week @ 2021-01-28 84992/week @ 2021-02-04 86417/week @ 2021-02-11 89737/week @ 2021-02-18 95668/week @ 2021-02-25 95243/week @ 2021-03-04 104720/week @ 2021-03-11 100295/week @ 2021-03-18 107656/week @ 2021-03-25 94461/week @ 2021-04-01 102448/week @ 2021-04-08 100974/week @ 2021-04-15 95887/week @ 2021-04-22 78207/week @ 2021-04-29

368,383 downloads per month
Used in 2,880 crates (185 directly)

MIT/Apache

2MB
35K 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.3MB
~27K SLoC