Struct RawPromise

Source
pub struct RawPromise { /* private fields */ }
Expand description

Wrapper for a JS promise, working with stdweb::Values.

This provides a thin layer over JS promise objects, so that the user does not have to worry about using the js!{} macro for calling then or ensuring the rust callback functions are freed.

To wrap an existing promise created using js!{}, use the from_reference or from_value functions. You can also use new_resolved or new_rejected to create pre-resolved promises.

Note that if the promise is never resolved or rejected, it will leak memory, since the rust callbacks will never be freed.

Implementations§

Source§

impl RawPromise

Source

pub fn from_reference(p: Reference) -> Self

Wraps an existing promise.

This function does not check if the reference points to a valid promise object; if it doesn’t have a then method, methods on the wrapper will likely panic.

Source

pub fn from_value(v: Value) -> Self

Wraps an existing promise.

Panics if the value is not a reference.

This function does not check if the reference points to a valid promise object; if it doesn’t have a then method, methods on the wrapper will likely panic.

Source

pub fn js_obj(&self) -> &Reference

Gets the JS reference object to this promise.

Source

pub fn new_resolved(v: Value) -> Self

Creates a new promise, already resolved to a value.

Calls the JS Promise.resolve method.

Source

pub fn new_rejected(v: Value) -> Self

Creates a new promise, already rejected

Calls the JS Promise.reject method.

Source

pub fn from_result(v: Result<Value, Value>) -> Self

Creates a new, already fulfilled promise from a Result.

Either resolved if the passed-in Result is Ok, or rejected if it was Err.

Source

pub fn then<F: FnOnce(Result<Value, Value>) -> Value + Send + 'static>( &self, f: F, ) -> Self

Runs a function once the promise has resolved.

Instead of separate resolve and reject functions, the single callback is passed a Result object. The callback returns a JS value that is interpreted as usual for a promise return value.

§Panics

If this promise was created by from_reference with an invalid promise object (no then method, then didn’t return an object, etc).

Source

pub fn then_to_typed<TOk: Send, TErr: Send, F: FnOnce(Result<Value, Value>) -> PromiseResult<TOk, TErr> + Send + 'static>( &self, f: F, ) -> Promise<TOk, TErr>

Runs a function once the promise has resolved, returning Rust objects.

This is the main way to switch from JS promises to this crate’s typed Promise type.

§Panics

If this promise was created by from_reference with an invalid promise object (no then method, then didn’t return an object, etc).

Source

pub fn all<I: IntoIterator<Item = RawPromise>>(promises: I) -> RawPromise

Creates a new promise that either resolves when all promises passed to it resolve or rejects when at least one promise passed to it rejects.

Binding for JS’s Promise.all function; see its documentation for semantics.

Source

pub fn all_settled<I: IntoIterator<Item = RawPromise>>( promises: I, ) -> RawPromise

Creates a new promise that resolves when all promises passed to it have resolved or have been rejected.

Binding for JS’s Promsie.allSettled function; see its documentation for semantics.

Source

pub fn race<I: IntoIterator<Item = RawPromise>>(promises: I) -> RawPromise

Creates a new promise that waits until the any of the promises passed to it have been resolved or rejected, returning its result.

Binding for JS’s Promsie.race function; see its documentation for semantics.

Trait Implementations§

Source§

impl Clone for RawPromise

Source§

fn clone(&self) -> RawPromise

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.