Skip to main content

js_sys/futures/
mod.rs

1//! Converting between JavaScript `Promise`s to Rust `Future`s.
2//!
3//! This module provides a bridge for working with JavaScript `Promise` types as
4//! a Rust `Future`, and similarly contains utilities to turn a rust `Future`
5//! into a JavaScript `Promise`. This can be useful when working with
6//! asynchronous or otherwise blocking work in Rust (wasm), and provides the
7//! ability to interoperate with JavaScript events and JavaScript I/O
8//! primitives.
9//!
10//! There are three main interfaces in this module currently:
11//!
12//! 1. [**`JsFuture`**](./struct.JsFuture.html)
13//!
14//!    A type that is constructed with a `Promise` and can then be used as a
15//!    `Future<Output = Result<JsValue, JsValue>>`. This Rust future will resolve
16//!    or reject with the value coming out of the `Promise`.
17//!
18//! 2. [**`future_to_promise`**](./fn.future_to_promise.html)
19//!
20//!    Converts a Rust `Future<Output = Result<JsValue, JsValue>>` into a
21//!    JavaScript `Promise`. The future's result will translate to either a
22//!    resolved or rejected `Promise` in JavaScript.
23//!
24//! 3. [**`spawn_local`**](./fn.spawn_local.html)
25//!
26//!    Spawns a `Future<Output = ()>` on the current thread. This is the
27//!    best way to run a `Future` in Rust without sending it to JavaScript.
28//!
29//! These three items should provide enough of a bridge to interoperate the two
30//! systems and make sure that Rust/JavaScript can work together with
31//! asynchronous and I/O work.
32
33extern crate alloc;
34
35#[cfg(not(target_feature = "atomics"))]
36mod jspi;
37
38use crate::Promise;
39use alloc::rc::Rc;
40use core::cell::RefCell;
41use core::fmt;
42use core::future::{Future, IntoFuture};
43use core::panic::AssertUnwindSafe;
44use core::pin::Pin;
45use core::task::{Context, Poll, Waker};
46#[cfg(all(
47    all(target_family = "wasm", not(target_os = "wasi")),
48    feature = "std",
49    panic = "unwind"
50))]
51use futures_util::FutureExt;
52use wasm_bindgen::__rt::marker::ErasableGeneric;
53#[cfg(all(
54    all(target_family = "wasm", not(target_os = "wasi")),
55    feature = "std",
56    panic = "unwind"
57))]
58use wasm_bindgen::__rt::panic_to_panic_error;
59use wasm_bindgen::convert::{FromWasmAbi, Upcast};
60use wasm_bindgen::sys::Promising;
61use wasm_bindgen::{prelude::*, JsError, JsGeneric};
62
63#[cfg_attr(docsrs, doc(cfg(feature = "futures-core-03-stream")))]
64#[cfg(feature = "futures-core-03-stream")]
65pub mod stream;
66
67mod queue;
68
69mod task {
70    use cfg_if::cfg_if;
71
72    cfg_if! {
73        if #[cfg(target_feature = "atomics")] {
74            mod wait_async_polyfill;
75            mod multithread;
76            pub(crate) use multithread::*;
77
78        } else {
79            mod singlethread;
80            pub(crate) use singlethread::*;
81         }
82    }
83}
84
85/// Runs a Rust `Future` on the current thread.
86///
87/// The `future` must be `'static` because it will be scheduled
88/// to run in the background and cannot contain any stack references.
89///
90/// The `future` will always be run on the next microtask tick even if it
91/// immediately returns `Poll::Ready`.
92///
93/// # JSPI
94///
95/// When called from within a JSPI context — a `#[wasm_bindgen(jspi)]` export
96/// or a task itself spawned from one — the task's polls are entered through
97/// a `WebAssembly.promising` boundary, so sync code reached from the future
98/// may suspend via [`jspi_block_on_promise`]. The capability is inherited
99/// transitively down the spawn tree; a suspension parks only that task's
100/// poll. In modules that never use JSPI attributes this branch compiles to a
101/// constant and no JSPI machinery is emitted.
102///
103/// # Panics
104///
105/// This function has the same panic behavior as `future_to_promise`.
106#[inline]
107pub fn spawn_local<F>(future: F)
108where
109    F: Future<Output = ()> + 'static,
110{
111    #[cfg(not(target_feature = "atomics"))]
112    if jspi::in_context() {
113        jspi::spawn_promising(future);
114        return;
115    }
116    task::Task::spawn(future);
117}
118
119/// Suspend the current JSPI execution until `promise` settles, returning the
120/// resolved value as `Ok` or the rejection reason as `Err` — without
121/// blocking the event loop, and without an `async` call chain.
122///
123/// May only be called where a `WebAssembly.promising` frame is on the stack:
124/// within a `#[wasm_bindgen(jspi)]` export, or a task spawned (transitively)
125/// from a JSPI context. Calling it elsewhere throws a `SuspendError` at
126/// runtime.
127///
128/// Promises are eager, so concurrency composes at the promise level: start
129/// several JS calls, then suspend on each or on a `Promise::all` /
130/// `Promise::race` combination. A Rust `Future` is awaited by suspending on
131/// its completion promise: `jspi_block_on_promise(&future_to_promise(fut))`.
132#[cfg(not(target_feature = "atomics"))]
133#[deprecated(note = "JSPI support is experimental and subject to change; \
134            `jspi_block_on_promise` requires a runtime with WebAssembly \
135            JS Promise Integration enabled")]
136pub fn jspi_block_on_promise(promise: &Promise) -> Result<JsValue, JsValue> {
137    jspi::suspend(promise)
138}
139
140struct Inner<T = JsValue, E = JsValue> {
141    result: Option<Result<T, E>>,
142    task: Option<Waker>,
143    callbacks: Option<(
144        Closure<dyn FnMut(T) -> Result<(), JsError>>,
145        Closure<dyn FnMut(JsValue) -> Result<(), JsError>>,
146    )>,
147}
148
149/// A Rust `Future` backed by a JavaScript `Promise`.
150///
151/// This type is constructed with a JavaScript `Promise` object and translates
152/// it to a Rust `Future`. This type implements the `Future` trait from the
153/// `futures` crate and will either succeed or fail depending on what happens
154/// with the JavaScript `Promise`.
155///
156/// A `JsFuture<T, E>` built from a `Promise<T, E>` resolves to
157/// `Result<T, E>`. The fulfilled value is converted as a `T`, unchecked, as
158/// any declared return is. The rejection reason is whatever JS threw, so it
159/// converts through `E: From<JsValue>`, as a `catch` import's error does.
160///
161/// Currently this type is constructed with `JsFuture::from`.
162pub struct JsFuture<T = JsValue, E = JsValue> {
163    inner: Rc<RefCell<Inner<T, E>>>,
164}
165
166impl<T: core::panic::UnwindSafe, E: core::panic::UnwindSafe> core::panic::UnwindSafe
167    for JsFuture<T, E>
168{
169}
170
171unsafe impl<T, E> ErasableGeneric for JsFuture<T, E> {
172    type Repr = JsFuture<JsValue, JsValue>;
173}
174
175// Covariant in both the success and the rejection type, like `Promise<T, E>`.
176impl<T, E, Target, ErrTarget> Upcast<JsFuture<Target, ErrTarget>> for JsFuture<T, E>
177where
178    T: Upcast<Target>,
179    E: Upcast<ErrTarget>,
180{
181}
182
183impl<T, E> fmt::Debug for JsFuture<T, E> {
184    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
185        write!(f, "JsFuture {{ ... }}")
186    }
187}
188
189// `FromWasmAbi` is what the closure shim invokes on the resolved value;
190// no layout equivalence with `JsValue` is required at this seam — the
191// per-type `from_abi` does the conversion (e.g. for dynamic unions it
192// runs the variant dispatcher).
193impl<T: FromWasmAbi + 'static, E: From<JsValue> + 'static> From<Promise<T, E>> for JsFuture<T, E> {
194    fn from(js: Promise<T, E>) -> JsFuture<T, E> {
195        // Use the `then` method to schedule two callbacks, one for the
196        // resolved value and one for the rejected value. We're currently
197        // assuming that JS engines will unconditionally invoke precisely one of
198        // these callbacks, no matter what.
199        //
200        // Ideally we'd have a way to cancel the callbacks getting invoked and
201        // free up state ourselves when this `JsFuture` is dropped. We don't
202        // have that, though, and one of the callbacks is likely always going to
203        // be invoked.
204        //
205        // As a result we need to make sure that no matter when the callbacks
206        // are invoked they are valid to be called at any time, which means they
207        // have to be self-contained. Through the `Closure::once` and some
208        // `Rc`-trickery we can arrange for both instances of `Closure`, and the
209        // `Rc`, to all be destroyed once the first one is called.
210        let state = Rc::new(RefCell::new(Inner::<T, E> {
211            result: None,
212            task: None,
213            callbacks: None,
214        }));
215
216        fn finish<T, E>(state: &RefCell<Inner<T, E>>, val: Result<T, E>) {
217            let task = {
218                let mut state = state.borrow_mut();
219                assert!(
220                    state.callbacks.is_some(),
221                    "finish: callbacks should be Some"
222                );
223                assert!(state.result.is_none(), "finish: result should be None");
224
225                // First up drop our closures as they'll never be invoked again and
226                // this is our chance to clean up their state.
227                drop(state.callbacks.take());
228
229                // Next, store the value into the internal state.
230                state.result = Some(val);
231                state.task.take()
232            };
233
234            // And then finally if any task was waiting on the value wake it up and
235            // let them know it's there.
236            if let Some(task) = task {
237                task.wake()
238            }
239        }
240
241        let resolve = {
242            let state = AssertUnwindSafe(state.clone());
243            Closure::once(move |val: T| {
244                finish(&*state, Ok(val));
245                Ok(())
246            })
247        };
248
249        let reject = {
250            let state = AssertUnwindSafe(state.clone());
251            Closure::once(move |val: JsValue| {
252                finish(&*state, Err(E::from(val)));
253                Ok(())
254            })
255        };
256
257        let js: Promise<T> = js.unchecked_into();
258        let _ = js.then_with_reject(&resolve, &reject);
259
260        state.borrow_mut().callbacks = Some((resolve, reject));
261
262        JsFuture { inner: state }
263    }
264}
265
266impl<T, E> Future for JsFuture<T, E> {
267    type Output = Result<T, E>;
268
269    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
270        let mut inner = self.inner.borrow_mut();
271
272        // If our value has come in then we return it...
273        if let Some(val) = inner.result.take() {
274            return Poll::Ready(val);
275        }
276
277        // ... otherwise we arrange ourselves to get woken up once the value
278        // does come in
279        inner.task = Some(cx.waker().clone());
280        Poll::Pending
281    }
282}
283
284impl<T: FromWasmAbi + 'static, E: From<JsValue> + 'static> IntoFuture for Promise<T, E> {
285    type Output = Result<T, E>;
286    type IntoFuture = JsFuture<T, E>;
287
288    fn into_future(self) -> JsFuture<T, E> {
289        JsFuture::from(self)
290    }
291}
292
293/// Converts a Rust `Future` into a JavaScript `Promise`.
294///
295/// This function will take any future in Rust and schedule it to be executed,
296/// returning a JavaScript `Promise` which can then be passed to JavaScript.
297///
298/// The `future` must be `'static` because it will be scheduled to run in the
299/// background and cannot contain any stack references.
300///
301/// The returned `Promise` will be resolved or rejected when the future
302/// completes, depending on whether it finishes with `Ok` or `Err`.
303///
304/// # Panics
305///
306/// Note that in Wasm panics are currently translated to aborts, but "abort" in
307/// this case means that a JavaScript exception is thrown. The Wasm module is
308/// still usable (likely erroneously) after Rust panics.
309#[cfg(not(all(
310    all(target_family = "wasm", not(target_os = "wasi")),
311    feature = "std",
312    panic = "unwind"
313)))]
314pub fn future_to_promise<F>(future: F) -> Promise
315where
316    F: Future<Output = Result<JsValue, JsValue>> + 'static,
317{
318    let mut future = Some(future);
319
320    Promise::new_typed(&mut move |resolve, reject| {
321        let future = future.take().unwrap_throw();
322
323        spawn_local(async move {
324            match future.await {
325                Ok(val) => {
326                    resolve.call(&JsValue::UNDEFINED, (&val,)).unwrap_throw();
327                }
328                Err(val) => {
329                    reject.call(&JsValue::UNDEFINED, (&val,)).unwrap_throw();
330                }
331            }
332        });
333    })
334}
335
336/// Converts a Rust `Future` into a JavaScript `Promise`.
337///
338/// This function will take any future in Rust and schedule it to be executed,
339/// returning a JavaScript `Promise` which can then be passed to JavaScript.
340///
341/// The `future` must be `'static` because it will be scheduled to run in the
342/// background and cannot contain any stack references.
343///
344/// The returned `Promise` will be resolved or rejected when the future
345/// completes, depending on whether it finishes with `Ok` or `Err`.
346///
347/// # Panics
348///
349/// If the `future` provided panics then the returned `Promise` will be rejected
350/// with a PanicError.
351#[cfg(all(
352    all(target_family = "wasm", not(target_os = "wasi")),
353    feature = "std",
354    panic = "unwind"
355))]
356pub fn future_to_promise<F>(future: F) -> Promise
357where
358    F: Future<Output = Result<JsValue, JsValue>> + 'static + std::panic::UnwindSafe,
359{
360    // Wrap `future` in AssertUnwindSafe and move it into the closure so the closure
361    // satisfies MaybeUnwindSafe (required when panic=unwind). Using `move` avoids
362    // capturing a `&mut` reference, which is never UnwindSafe. The Promise executor
363    // is not called inside a panic-catching context, so this is always safe.
364    let mut future = core::panic::AssertUnwindSafe(Some(future));
365    Promise::new(&mut move |resolve, reject| {
366        let future = future.take().unwrap_throw();
367        spawn_local(async move {
368            let res = future.catch_unwind().await;
369            match res {
370                Ok(Ok(val)) => {
371                    resolve.call(&JsValue::UNDEFINED, (&val,)).unwrap_throw();
372                }
373                Ok(Err(val)) => {
374                    reject.call(&JsValue::UNDEFINED, (&val,)).unwrap_throw();
375                }
376                Err(val) => {
377                    reject
378                        .call(&JsValue::UNDEFINED, (&panic_to_panic_error(val),))
379                        .unwrap_throw();
380                }
381            }
382        });
383    })
384}
385
386// Note: Once we bump MSRV, we can type future_to_promise with backwards compatible inference.
387/// Converts a Rust `Future` into a corresponding typed JavaScript `Promise<T>`.
388///
389/// This function will take any future in Rust and schedule it to be executed,
390/// returning a JavaScript `Promise` which can then be passed to JavaScript.
391///
392/// The `future` must be `'static` because it will be scheduled to run in the
393/// background and cannot contain any stack references.
394///
395/// The returned `Promise` will be resolved or rejected when the future completes,
396/// depending on whether it finishes with `Ok` or `Err`.
397///
398/// # Panics
399///
400/// Note that in Wasm panics are currently translated to aborts, but "abort" in
401/// this case means that a JavaScript exception is thrown. The Wasm module is
402/// still usable (likely erroneously) after Rust panics.
403///
404/// If the `future` provided panics then the returned `Promise` **will not
405/// resolve**. Instead it will be a leaked promise. This is an unfortunate
406/// limitation of Wasm currently that's hoped to be fixed one day!
407pub fn future_to_promise_typed<T, F>(future: F) -> Promise<<T as Promising>::Resolution>
408where
409    F: Future<Output = Result<T, JsValue>> + 'static,
410    T: Promising + FromWasmAbi + JsGeneric,
411{
412    let mut future = Some(future);
413
414    Promise::new_typed(&mut move |resolve, reject| {
415        let future = future.take().unwrap_throw();
416        spawn_local(async move {
417            match future.await {
418                Ok(val) => {
419                    resolve.call(&JsValue::UNDEFINED, (&val,)).unwrap_throw();
420                }
421                Err(val) => {
422                    reject.call(&JsValue::UNDEFINED, (&val,)).unwrap_throw();
423                }
424            }
425        });
426    })
427}