Struct Promise

#[must_use]
#[repr(transparent)]
pub struct Promise<T = JsValue> { /* private fields */ }

The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.

MDN documentation

Implementations

impl Promise

fn new_typed<T>(cb: &mut dyn FnMut(Function<fn(T) -> Undefined>, Function<fn(JsValue) -> Undefined>)) -> Promise<<T as Promising>::Resolution>
where
    T: Promising + JsGeneric,
    dyn FnMut(Function<fn(T) -> Undefined>, Function<fn(JsValue) -> Undefined>): ErasableGenericBorrowMut<dyn FnMut(Function<fn(JsValue) -> Undefined>, Function<fn(JsValue) -> Undefined>)>,
    Promise<<T as Promising>::Resolution>: ErasableGenericOwn<Promise<<JsValue as Promising>::Resolution>>,

Creates a new Promise with the provided executor cb

MDN documentation

impl Promise

fn all_settled(obj: &JsValue) -> Promise

The Promise.allSettled(iterable) method returns a single Promise that resolves when all of the promises in the iterable argument have either fulfilled or rejected or when the iterable argument contains no promises.

MDN documentation

impl Promise

fn any(obj: &JsValue) -> Promise

The Promise.any(iterable) method returns a single Promise that resolves when any of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with an AggregateError if all promises in the iterable rejected.

MDN documentation

impl Promise

fn race_iterable<I>(obj: &I) -> Promise<<I::Item as Promising>::Resolution>
where
    I: Iterable + ErasableGenericBorrow<JsValue>,
    I::Item: Promising,
    Promise<<I::Item as Promising>::Resolution>: ErasableGenericOwn<Promise<<JsValue as Promising>::Resolution>>,

The Promise.race(iterable) method returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise.

MDN documentation

impl Promise

fn resolve<U>(obj: &U) -> Promise<U::Resolution>
where
    U: Promising + ErasableGenericBorrow<JsValue>,
    Promise<U::Resolution>: ErasableGenericOwn<Promise<JsValue>>,

The Promise.resolve(value) method returns a Promise object that is resolved with the given value. If the value is a promise, that promise is returned; if the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value.

MDN documentation

impl Promise

fn then2(&self, resolve: &ScopedClosure<'_, dyn FnMut(JsValue)>, reject: &ScopedClosure<'_, dyn FnMut(JsValue)>) -> Promise

Same as then, only with both arguments provided.

MDN documentation

impl Promise

fn all_tuple<T: PromiseTuple>(promises: T) -> Promise<T::Joined>

Heterogeneous counterpart to [Promise::all_iterable]: accepts a Rust tuple of Promise<T_i> and returns a single Promise resolving to a typed [ArrayTuple<(T_1, T_2, ..., T_n)>].

Destructure the awaited result via ArrayTuple::into_tuple to get the individual values back as a native Rust tuple. Implemented for arity 1..=8.

Rejects with the first rejection, matching Promise.all semantics.

Example

use js_sys::Promise;

let (response, buffer) = Promise::all_tuple((fetch_promise, buffer_promise))
    .await?
    .into_tuple();
fn all_settled_tuple<T: PromiseTuple>(promises: T) -> Promise<T::Settled>

Heterogeneous counterpart to [Promise::all_settled_iterable]: accepts a Rust tuple of Promise<T_i> and returns a single Promise resolving to a typed ArrayTuple<(PromiseState<T_1>, ..., PromiseState<T_n>)>.

Unlike Promise::all_tuple, this never rejects early: every input settles (fulfills or rejects) and is reflected by its PromiseState slot in the result tuple. Implemented for arity 1..=8.

Example

use js_sys::Promise;

let results = Promise::all_settled_tuple((fetch_promise, buffer_promise)).await?;
let (response_state, buffer_state) = results.into_tuple();

impl Promise

fn all(obj: &JsValue) -> Promise

The Promise.all(iterable) method returns a single Promise that resolves when all of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with the reason of the first promise that rejects.

MDN documentation

impl Promise

fn all_settled_iterable<I>(obj: &I) -> Promise<Array<PromiseState<<I::Item as Promising>::Resolution>>>
where
    I: Iterable + ErasableGenericBorrow<JsValue>,
    I::Item: Promising,
    Promise<Array<PromiseState<<I::Item as Promising>::Resolution>>>: ErasableGenericOwn<Promise<Array<PromiseState<<JsValue as Promising>::Resolution>>>>,

The Promise.allSettled(iterable) method returns a single Promise that resolves when all of the promises in the iterable argument have either fulfilled or rejected or when the iterable argument contains no promises.

MDN documentation

impl Promise

fn any_iterable<I>(obj: &I) -> Promise<<I::Item as Promising>::Resolution>
where
    I: Iterable + ErasableGenericBorrow<JsValue>,
    I::Item: Promising,
    Promise<<I::Item as Promising>::Resolution>: ErasableGenericOwn<Promise<<JsValue as Promising>::Resolution>>,

The Promise.any(iterable) method returns a single Promise that resolves when any of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with an AggregateError if all promises in the iterable rejected.

MDN documentation

impl Promise

fn reject(obj: &JsValue) -> Promise

The Promise.reject(reason) method returns a Promise object that is rejected with the given reason.

MDN documentation

impl Promise

fn new(cb: &mut impl FnMut(Function, Function) + MaybeUnwindSafe) -> Promise

Creates a new Promise with the provided executor cb

The cb is a function that is passed with the arguments resolve and reject. The cb function is executed immediately by the Promise implementation, passing resolve and reject functions (the executor is called before the Promise constructor even returns the created object). The resolve and reject functions, when called, resolve or reject the promise, respectively. The executor normally initiates some asynchronous work, and then, once that completes, either calls the resolve function to resolve the promise or else rejects it if an error occurred.

If an error is thrown in the executor function, the promise is rejected. The return value of the executor is ignored.

MDN documentation

impl Promise

fn all_iterable<I>(obj: &I) -> Promise<Array<<I::Item as Promising>::Resolution>>
where
    I: Iterable + ErasableGenericBorrow<JsValue>,
    I::Item: Promising,
    Promise<Array<<I::Item as Promising>::Resolution>>: ErasableGenericOwn<Promise<Array<<JsValue as Promising>::Resolution>>>,

The Promise.all(iterable) method returns a single Promise that resolves when all of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with the reason of the first promise that rejects.

MDN documentation

impl Promise

fn race(obj: &JsValue) -> Promise

The Promise.race(iterable) method returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise.

MDN documentation

impl<T> Promise<T>

fn then<'a>(&self, cb: &ScopedClosure<'a, dyn FnMut(T)>) -> Promise<JsValue>
where
    ScopedClosure<'a, dyn FnMut(T)>: ErasableGenericBorrow<ScopedClosure<'static, dyn FnMut(JsValue)>>,

The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise.

MDN documentation

impl<T> Promise<T>

fn catch(&self, cb: &ScopedClosure<'_, dyn FnMut(JsValue)>) -> Promise<JsValue>

The catch() method returns a Promise and deals with rejected cases only. It behaves the same as calling Promise.prototype.then(undefined, onRejected) (in fact, calling obj.catch(onRejected) internally calls obj.then(undefined, onRejected)).

MDN documentation

impl<T> Promise<T>

fn then_with_reject<'a, R>(&self, resolve: &ScopedClosure<'a, dyn FnMut(T) -> Result<R, JsError>>, reject: &ScopedClosure<'a, dyn FnMut(JsValue) -> Result<R, JsError>>) -> Promise<R::Resolution>
where
    R: Promising,
    ScopedClosure<'a, dyn FnMut(T) -> Result<R, JsError>>: ErasableGenericBorrow<ScopedClosure<'static, dyn FnMut(JsValue) -> Result<JsValue, JsError>>>,
    ScopedClosure<'a, dyn FnMut(JsValue) -> Result<R, JsError>>: ErasableGenericBorrow<ScopedClosure<'static, dyn FnMut(JsValue) -> Result<JsValue, JsError>>>,
    Promise<R::Resolution>: ErasableGenericOwn<Promise<JsValue>>,

The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise.

MDN documentation

impl<T> Promise<T>

fn finally(&self, cb: &ScopedClosure<'_, dyn FnMut()>) -> Promise<JsValue>

The finally() method returns a Promise. When the promise is settled, whether fulfilled or rejected, the specified callback function is executed. This provides a way for code that must be executed once the Promise has been dealt with to be run whether the promise was fulfilled successfully or rejected.

This lets you avoid duplicating code in both the promise's then() and catch() handlers.

MDN documentation

impl<T> Promise<T>

fn reject_typed(obj: &JsValue) -> Promise<T>
where
    Promise<T>: ErasableGenericOwn<Promise<JsValue>>,

The Promise.reject(reason) method returns a Promise object that is rejected with the given reason.

MDN documentation

impl<T> Promise<T>

fn catch_map<'a, R>(&self, cb: &ScopedClosure<'a, dyn FnMut(T) -> Result<R, JsError>>) -> Promise<R::Resolution>
where
    R: Promising,
    ScopedClosure<'a, dyn FnMut(T) -> Result<R, JsError>>: ErasableGenericBorrow<ScopedClosure<'static, dyn FnMut(JsValue) -> Result<JsValue, JsError>>>,
    Promise<R::Resolution>: ErasableGenericOwn<Promise<JsValue>>,

Same as catch, but returning a result to become the new Promise value.

impl<T> Promise<T>

fn then_map<'a, R>(&self, cb: &ScopedClosure<'a, dyn FnMut(T) -> Result<R, JsError>>) -> Promise<R::Resolution>
where
    R: Promising,
    ScopedClosure<'a, dyn FnMut(T) -> Result<R, JsError>>: ErasableGenericBorrow<ScopedClosure<'static, dyn FnMut(JsValue) -> Result<JsValue, JsError>>>,
    Promise<R::Resolution>: ErasableGenericOwn<Promise<JsValue>>,

Alias for then() with a return value. The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise.

MDN documentation

Trait Implementations

impl From<JsValue> for Promise

fn from(obj: JsValue) -> Self

impl<T> AsRef<JsValue> for Promise<T>

fn as_ref(&self) -> &JsValue

impl<T> AsRef<Object> for Promise<T>

fn as_ref(&self) -> &Object

impl<T> AsRef<Promise<T>> for Promise<T>

fn as_ref(&self) -> &Promise<T>

impl<T> Deref for Promise<T>

type Target = Object;
fn deref(&self) -> &Object

impl<T> ErasableGeneric for Promise<T>

type Repr = JsValue;

impl<T> FromWasmAbi for Promise<T>

type Abi = <JsValue as FromWasmAbi>::Abi;
unsafe fn from_abi(js: Self::Abi) -> Self

impl<T> IntoJsGeneric for Promise<T> where Promise<T>: JsGeneric,

type JsCanon = Promise<T>;
fn to_js(self) -> Promise<T>

impl<T> IntoWasmAbi for Promise<T>

type Abi = <JsValue as IntoWasmAbi>::Abi;
fn into_abi(self) -> Self::Abi

impl<T> JsCast for Promise<T>

fn instanceof(val: &JsValue) -> bool
fn unchecked_from_js(val: JsValue) -> Self
fn unchecked_from_js_ref(val: &JsValue) -> &Self

impl<T> LongRefFromWasmAbi for Promise<T>

type Abi = <JsValue as LongRefFromWasmAbi>::Abi;
type Anchor = Promise<T>;
unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor

impl<T> OptionFromWasmAbi for Promise<T>

fn is_none(abi: &Self::Abi) -> bool

impl<T> OptionIntoWasmAbi for Promise<T>

fn none() -> Self::Abi

impl<T> RefFromWasmAbi for Promise<T>

type Abi = <JsValue as RefFromWasmAbi>::Abi;
type Anchor = ManuallyDrop<Promise<T>>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor

impl<T> WasmDescribe for Promise<T>

fn describe()

impl<T, __UpcastTarget0> UpcastFrom<Promise<T>> for Promise<__UpcastTarget0> where __UpcastTarget0: UpcastFrom<T>,

impl<T: Clone> Clone for Promise<T>

fn clone(&self) -> Self

impl<T: Debug> Debug for Promise<T>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<T: FromWasmAbi + 'static> IntoFuture for Promise<T>

type Output = Result<T, JsValue>;
type IntoFuture = JsFuture<T>;
fn into_future(self) -> JsFuture<T>

impl<T: JsGeneric> Promising for Promise<T>

type Resolution = T;

Auto Trait Implementations

impl<T> Freeze for Promise<T> where PhantomData<(T,)>: Freeze,

impl<T> RefUnwindSafe for Promise<T> where PhantomData<(T,)>: RefUnwindSafe,

impl<T> Send for Promise<T> where PhantomData<(T,)>: Send,

impl<T> Sync for Promise<T> where PhantomData<(T,)>: Sync,

impl<T> Unpin for Promise<T> where PhantomData<(T,)>: Unpin,

impl<T> UnsafeUnpin for Promise<T> where PhantomData<(T,)>: UnsafeUnpin,

impl<T> UnwindSafe for Promise<T> where PhantomData<(T,)>: UnwindSafe,

Blanket Implementations

impl<P, T> Receiver for Promise<T> where P: Deref<Target = T> + ?Sized, T: ?Sized,

impl<S, T> Upcast<T> for Promise<T> where T: UpcastFrom<S> + ?Sized, S: ?Sized,

impl<T> Any for Promise<T> where T: 'static + ?Sized,

impl<T> Borrow<T> for Promise<T> where T: ?Sized,

impl<T> BorrowMut<T> for Promise<T> where T: ?Sized,

impl<T> CloneToUninit for Promise<T> where T: Clone,

impl<T> From<T> for Promise<T>

impl<T> JsGeneric for Promise<T> where T: ErasableGeneric<Repr = JsValue> + UpcastFrom<T> + Upcast<JsValue> + JsCast + 'static,

impl<T> ReturnWasmAbi for Promise<T> where T: IntoWasmAbi,

impl<T> ToOwned for Promise<T> where T: Clone,

impl<T> TryFromJsValue for Promise<T> where T: JsCast,

impl<T> VectorFromWasmAbi for Promise<T> where T: ErasableGeneric<Repr = JsValue> + WasmDescribe,

impl<T> VectorIntoWasmAbi for Promise<T> where T: ErasableGeneric<Repr = JsValue> + WasmDescribe,

impl<T> VectorRefIntoWasmAbi for Promise<T> where &'a T: for<'a> Into<JsValue>,

impl<T, U> Into<U> for Promise<T> where U: From<T>,

impl<T, U> TryFrom<U> for Promise<T> where U: Into<T>,

impl<T, U> TryInto<U> for Promise<T> where U: TryFrom<T>,