Pending<T, E>
Package: antithrow
A Result that has not settled yet. Implements PromiseLike<Settled<T, E>>, so it can be awaited.
Signature
class Pending<out T, out E>
extends ResultBase<T, E>
implements PromiseLike<Settled<T, E>>
{
constructor(promise: PromiseLike<Settled<T, E>>);
readonly promise: PromiseLike<Settled<T, E>>;
then<A = Settled<T, E>, B = never>(
onfulfilled?: ((value: Settled<T, E>) => A | PromiseLike<A>) | null,
onrejected?: ((reason: unknown) => B | PromiseLike<B>) | null,
): PromiseLike<A | B>;
}
| Parameter | Description |
|---|---|
T | The success value type. |
E | The error type. |
Constructor
new Pending<T, E>(promise: PromiseLike<Settled<T, E>>): Pending<T, E>;
| Argument | Type | Description |
|---|---|---|
promise | PromiseLike<Settled<T, E>> | The underlying promise. Must always resolve; rejections are not caught by Pending itself. |
Use Result.fromPromise or Result.try to construct a Pending from arbitrary code instead of the raw constructor.
Properties
| Name | Type | Description |
|---|---|---|
promise | PromiseLike<Settled<T, E>> | The underlying promise. |
Instance methods
isOk(): false— alwaysfalse.isErr(): false— alwaysfalse.isPending(): true— alwaystrue; narrowsthistoPending<T, E>.then(...)— standardPromiseLike.then. Resolves toSettled<T, E>.settle(): PromiseLike<Settled<T, E>>— alias for the inner promise.unwrap(): PromiseLike<T>— settles then callsunwrapon the result.unwrapErr(): PromiseLike<E>— settles then callsunwrapErr.unwrapOr(value: T): PromiseLike<T>— settles then falls back tovalue.unwrapOrElse(fn): PromiseLike<T>— settles then callsfnonErr.
All chainable methods (map, mapErr, andThen, and, or, orElse, flatten, mapOr, mapOrElse) return a new Pending that awaits the inner promise before applying the operation. See methods.
Pending also implements [Symbol.asyncIterator] so Result.do with an async generator can yield* it.
Throws
Never directly. Awaiting unwrap() or unwrapErr() on the wrong branch throws UnwrapError (the thrown value is observed through the promise rejection).
Example
import { Result } from "antithrow";
const pending = Result.try<string, Error>(async () => "hello");
const settled = await pending;
if (settled.isOk()) {
settled.value; // "hello"
}
See also
OkandErr— the settled shapes.Settled— theOk | Errnarrowing.- Chainable methods.