[][src]Struct js_promises::RawPromise

pub struct RawPromise { /* fields omitted */ }

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.

Methods

impl RawPromise[src]

pub fn from_reference(p: Reference) -> Self[src]

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.

pub fn from_value(v: Value) -> Self[src]

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.

pub fn js_obj(&self) -> &Reference[src]

Gets the JS reference object to this promise.

pub fn new_resolved(v: Value) -> Self[src]

Creates a new promise, already resolved to a value.

Calls the JS Promise.resolve method.

pub fn new_rejected(v: Value) -> Self[src]

Creates a new promise, already rejected

Calls the JS Promise.reject method.

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

Creates a new, already fulfilled promise from a Result.

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

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

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).

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>
[src]

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).

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

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.

pub fn all_settled<I: IntoIterator<Item = RawPromise>>(
    promises: I
) -> RawPromise
[src]

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.

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

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

impl Clone for RawPromise[src]

Auto Trait Implementations

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.