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.
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
Promisewith the provided executorcb
impl Promise
fn all_settled(obj: &JsValue) -> PromiseThe
Promise.allSettled(iterable)method returns a singlePromisethat resolves when all of the promises in the iterable argument have either fulfilled or rejected or when the iterable argument contains no promises.
impl Promise
fn any(obj: &JsValue) -> PromiseThe
Promise.any(iterable)method returns a singlePromisethat resolves when any of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with anAggregateErrorif all promises in the iterable rejected.
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.
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 aPromiseobject 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.
impl Promise
fn then2(&self, resolve: &ScopedClosure<'_, dyn FnMut(JsValue)>, reject: &ScopedClosure<'_, dyn FnMut(JsValue)>) -> PromiseSame as
then, only with both arguments provided.
impl Promise
fn all_tuple<T: PromiseTuple>(promises: T) -> Promise<T::Joined>Heterogeneous counterpart to [
Promise::all_iterable]: accepts a Rust tuple ofPromise<T_i>and returns a singlePromiseresolving to a typed [ArrayTuple<(T_1, T_2, ..., T_n)>].Destructure the awaited result via
ArrayTuple::into_tupleto get the individual values back as a native Rust tuple. Implemented for arity 1..=8.Rejects with the first rejection, matching
Promise.allsemantics.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 ofPromise<T_i>and returns a singlePromiseresolving to a typedArrayTuple<(PromiseState<T_1>, ..., PromiseState<T_n>)>.Unlike
Promise::all_tuple, this never rejects early: every input settles (fulfills or rejects) and is reflected by itsPromiseStateslot 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) -> PromiseThe
Promise.all(iterable)method returns a singlePromisethat 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.
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 singlePromisethat resolves when all of the promises in the iterable argument have either fulfilled or rejected or when the iterable argument contains no promises.
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 singlePromisethat resolves when any of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with anAggregateErrorif all promises in the iterable rejected.
impl Promise
fn reject(obj: &JsValue) -> PromiseThe
Promise.reject(reason)method returns aPromiseobject that is rejected with the given reason.
impl Promise
fn new(cb: &mut impl FnMut(Function, Function) + MaybeUnwindSafe) -> PromiseCreates a new
Promisewith the provided executorcbThe
cbis a function that is passed with the argumentsresolveandreject. Thecbfunction is executed immediately by thePromiseimplementation, passingresolveandrejectfunctions (the executor is called before thePromiseconstructor even returns the created object). Theresolveandrejectfunctions, when called, resolve or reject the promise, respectively. The executor normally initiates some asynchronous work, and then, once that completes, either calls theresolvefunction 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.
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 singlePromisethat 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.
impl Promise
fn race(obj: &JsValue) -> PromiseThe
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.
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 aPromise. It takes up to two arguments: callback functions for the success and failure cases of thePromise.
impl<T> Promise<T>
fn catch(&self, cb: &ScopedClosure<'_, dyn FnMut(JsValue)>) -> Promise<JsValue>The
catch()method returns aPromiseand deals with rejected cases only. It behaves the same as callingPromise.prototype.then(undefined, onRejected)(in fact, callingobj.catch(onRejected)internally callsobj.then(undefined, onRejected)).
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 aPromise. It takes up to two arguments: callback functions for the success and failure cases of thePromise.
impl<T> Promise<T>
fn finally(&self, cb: &ScopedClosure<'_, dyn FnMut()>) -> Promise<JsValue>The
finally()method returns aPromise. 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 thePromisehas 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()andcatch()handlers.
impl<T> Promise<T>
fn reject_typed(obj: &JsValue) -> Promise<T> where Promise<T>: ErasableGenericOwn<Promise<JsValue>>,The
Promise.reject(reason)method returns aPromiseobject that is rejected with the given reason.
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. Thethen()method returns aPromise. It takes up to two arguments: callback functions for the success and failure cases of thePromise.
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) -> boolfn unchecked_from_js(val: JsValue) -> Selffn 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;