85 releases

0.4.50 Jan 12, 2025
0.4.49 Dec 7, 2024
0.4.47 Nov 30, 2024
0.4.42 Mar 4, 2024
0.3.5 Nov 12, 2018

#1004 in WebAssembly

Download history 1087504/week @ 2025-01-25 1155306/week @ 2025-02-01 1273183/week @ 2025-02-08 1135941/week @ 2025-02-15 1259047/week @ 2025-02-22 1554929/week @ 2025-03-01 1531173/week @ 2025-03-08 1759142/week @ 2025-03-15 2062884/week @ 2025-03-22 1386019/week @ 2025-03-29 1449085/week @ 2025-04-05 1254142/week @ 2025-04-12 1249918/week @ 2025-04-19 1211749/week @ 2025-04-26 1276427/week @ 2025-05-03 1316812/week @ 2025-05-10

5,231,461 downloads per month
Used in 23,023 crates (1,018 directly)

MIT/Apache

2.5MB
43K SLoC

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.


wasm-bindgen-futures

API Documentation

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.

Dependencies

~0.6–1.2MB
~24K SLoC