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
impl RawPromise
Sourcepub fn from_reference(p: Reference) -> Self
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.
Sourcepub fn from_value(v: Value) -> Self
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.
Sourcepub fn new_resolved(v: Value) -> Self
pub fn new_resolved(v: Value) -> Self
Creates a new promise, already resolved to a value.
Calls the JS Promise.resolve method.
Sourcepub fn new_rejected(v: Value) -> Self
pub fn new_rejected(v: Value) -> Self
Creates a new promise, already rejected
Calls the JS Promise.reject method.
Sourcepub fn from_result(v: Result<Value, Value>) -> Self
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.
Sourcepub fn then<F: FnOnce(Result<Value, Value>) -> Value + Send + 'static>(
&self,
f: F,
) -> Self
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).
Sourcepub fn then_to_typed<TOk: Send, TErr: Send, F: FnOnce(Result<Value, Value>) -> PromiseResult<TOk, TErr> + Send + 'static>(
&self,
f: F,
) -> Promise<TOk, TErr>
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).
Sourcepub fn all<I: IntoIterator<Item = RawPromise>>(promises: I) -> RawPromise
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.
Sourcepub fn all_settled<I: IntoIterator<Item = RawPromise>>(
promises: I,
) -> RawPromise
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.
Sourcepub fn race<I: IntoIterator<Item = RawPromise>>(promises: I) -> RawPromise
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
impl Clone for RawPromise
Source§fn clone(&self) -> RawPromise
fn clone(&self) -> RawPromise
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more