Expand description
ECMAScript-style promises: owned futures with typed rejections.
A Promise<T, E> is a Future with Output = Result<T, E>
that owns its computation and remembers its outcome. It can be inspected
without consuming the result (Promise::peek, Promise::is_resolved,
Promise::is_rejected), polled manually (Promise::poll,
Promise::poll_sync), consumed (Promise::consume), or awaited.
Awaiting consumes the result; consuming a second time yields
PromiseRejection::already_consumed, except for task failures, which
replay through PromiseRejection::task_failed on every consumption.
For a promise that can be awaited any number of times, like its
ECMAScript counterpart, see Promise::shared.
The ECMAScript promise API maps onto:
Promise.resolveandPromise.reject:Promise::resolveandPromise::rejectnew Promise(executor):Promise::newPromise.withResolvers:Promise::with_resolversPromise.try:Promise::attemptandPromise::attempt_asyncPromise.all,Promise.any,Promise.race, andPromise.allSettled:Promise::all,Promise::any,Promise::race, andPromise::all_settledthen,catch, andfinally:Promise::then,Promise::catch, andPromise::finally, plusPromise::then_catchfor the two-argumentthen, andPromise::map,Promise::map_err,Promise::inspect, andPromise::inspect_err- thenable assimilation:
Promise::flatten
§Rejections
The rejection type E implements PromiseRejection, which lets a
promise synthesize rejections for consumption after the result was
already taken and for task failure. Panics inside a promise body are
caught and surface as rejections through
PromiseRejection::task_failed instead of unwinding into the caller.
See the trait documentation for the provided escape hatches.
§Scheduling
Without a runtime feature, every promise is lazy: the wrapped future
progresses only while the promise is polled, and dropping the promise
drops the future. With the tokio or smol feature enabled,
Promise::eager_or_lazy, and every combinator built on it
(Promise::then, Promise::map, Promise::catch, and the rest),
spawn the future on the runtime instead (falling back to lazy when only
tokio is enabled and no runtime context is active); a spawned future
runs to completion even if the promise is dropped.
Because Cargo unifies features across the whole build graph, any
dependency enabling tokio or smol flips this behavior for every
crate in the build. Do not rely on combinator laziness for correctness;
when laziness is required, construct the promise with Promise::lazy.
§Cargo features
tokio: eager scheduling viatokio::spawnand tokio-backed timers.smol: eager scheduling viasmol::spawnand smol-backed timers.anyhow: implementsPromiseRejectionforanyhow::Error.
Structs§
- Abort
Handle - Aborts the
Promisecreated byPromise::abortable. - Promise
- An owned future with a typed rejection, modeled on the ECMAScript
Promise. - Promise
Aborted - Best-effort success hint from
AbortHandle::abort. - Promise
Settled - Best-effort error hint from
AbortHandle::abort. - Reject
- Rejects the
Promisecreated byPromise::with_resolvers. - Resolve
- Resolves the
Promisecreated byPromise::with_resolvers. - Resolvers
Dropped - Rejection payload produced when every handle returned by
Promise::with_resolversis dropped without settling the promise. - Shared
Promise - A cloneable, multi-consumer handle to a
crate::Promise, created bycrate::Promise::shared.
Enums§
- Task
Failure - The cause of a task failure: the underlying task ended without producing a rejection value, e.g. it panicked or was cancelled.
- Wrapped
Promise Rejection - A ready-made
PromiseRejectionwrapping an arbitrary error typeE.
Traits§
- Promise
Rejection - A rejection type usable as the error of a
Promise.
Type Aliases§
- Boxed
Promise Future - The boxed future a pending
Promisedrives to completion.