Struct gj::Promise [] [src]

#[must_use]
pub struct Promise<T, E> where T: 'static, E: 'static {
    // some fields omitted
}

A computation that might eventually resolve to a value of type T or to an error of type E. Dropping the promise cancels the computation.

Methods

impl<T, E> Promise<T, E>
[src]

fn ok(value: T) -> Promise<T, E>

Creates a new promise that has already been fulfilled with the given value.

fn err(error: E) -> Promise<T, E>

Creates a new promise that has already been rejected with the given error.

fn never_done() -> Promise<T, E>

Creates a new promise that never resolves.

fn and_fulfiller() -> (Promise<T, E>, PromiseFulfiller<T, E>) where E: FulfillerDropped

Creates a new promise/fulfiller pair.

fn then_else<F, T1, E1>(self, func: F) -> Promise<T1, E1> where F: 'static, F: FnOnce(Result<T, E>) -> Promise<T1, E1>

Chains further computation to be executed once the promise resolves. When the promise is fulfilled or rejected, invokes func on its result.

If the returned promise is dropped before the chained computation runs, the chained computation will be cancelled.

Always returns immediately, even if the promise is already resolved. The earliest that func might be invoked is during the next turn() of the event loop.

fn then<F, T1>(self, func: F) -> Promise<T1, E> where F: 'static, F: FnOnce(T) -> Promise<T1, E>

Calls then_else() with a default error handler that simply propagates all errors.

fn map_else<F, T1, E1>(self, func: F) -> Promise<T1, E1> where F: 'static, F: FnOnce(Result<T, E>) -> Result<T1, E1>

Like then_else() but for a func that returns a direct value rather than a promise. As an optimization, execution of func is delayed until its result is known to be needed. The expectation here is that func is just doing some transformation on the results, not scheduling any other actions, and therefore the system does not need to be proactive about evaluating it. This way, a chain of trivial map() transformations can be executed all at once without repeated rescheduling through the event loop. Use the eagerly_evaluate() method to suppress this behavior.

fn map<F, R>(self, func: F) -> Promise<R, E> where F: 'static, F: FnOnce(T) -> Result<R, E>, R: 'static

Calls map_else() with a default error handler that simply propagates all errors.

fn map_err<E1, F>(self, func: F) -> Promise<T, E1> where F: 'static, F: FnOnce(E) -> E1

Transforms the error branch of the promise.

fn lift<E1>(self) -> Promise<T, E1> where E: Into<E1>

Maps errors into a more general type.

fn exclusive_join(self, other: Promise<T, E>) -> Promise<T, E>

Returns a new promise that resolves when either self or other resolves. The promise that doesn't resolve first is cancelled.

fn all<I>(promises: I) -> Promise<Vec<T>, E> where I: Iterator<Item=Promise<T, E>>

Transforms a collection of promises into a promise for a vector. If any of the promises fails, immediately cancels the remaining promises.

fn fork(self) -> ForkedPromise<T, E> where T: Clone, E: Clone

Forks the promise, so that multiple different clients can independently wait on the result.

fn attach<U>(self, value: U) -> Promise<T, E> where U: 'static

Holds onto value until the promise resolves, then drops value.

fn eagerly_evaluate(self) -> Promise<T, E>

Forces eager evaluation of this promise. Use this if you are going to hold on to the promise for a while without consuming the result, but you want to make sure that the system actually processes it.

fn wait(self, wait_scope: &WaitScope) -> Result<T, E>

Runs the event loop until the promise is fulfilled.

The WaitScope argument ensures that wait() can only be called at the top level of a program. Waiting within event callbacks is disallowed.