Skip to main content

Chainable methods

Package: antithrow

Every Result shape (Ok, Err, Pending) defines the same set of instance methods. This page is the consolidated reference.

Availability matrix

MethodOkErrPending
isOk / isErr / isPendingyesyesyes
maptransformsno-opPending (transforms on settle)
mapErrno-optransformsPending (transforms on settle)
mapOrtransformsreturns defaultPromiseLike
mapOrElsetransformsruns defaultFnPromiseLike
andThenruns fnshort-circuitsPending
andreturns othershort-circuitsPending
orshort-circuitsreturns otherPending
orElseshort-circuitsruns fnPending
flattenunwraps nestedno-opPending
unwrapreturns valuethrows UnwrapErrorPromiseLike<T>
unwrapErrthrows UnwrapErrorreturns errorPromiseLike<E>
unwrapOrreturns valuereturns defaultPromiseLike<T>
unwrapOrElsereturns valueruns fnPromiseLike<T>
settleresolves thisresolves thisresolves inner

Guards

isOk() / isErr() / isPending()

isOk(): this is Ok<T, E>;
isErr(): this is Err<T, E>;
isPending(): this is Pending<T, E>;

Type guards. Each returns true only for its own shape and narrows this in the branch. Safe to call on any result.

Value transformations

map(fn)

map<U>(fn: (value: T) => U | PromiseLike<U>): Result<U, E>;

Transforms the Ok value. Leaves Err untouched. If fn returns a promise, the result becomes Pending. Exceptions thrown by fn are not caught.

mapErr(fn)

mapErr<F>(fn: (error: E) => F | PromiseLike<F>): Result<T, F>;

Transforms the Err value. Leaves Ok untouched. If fn returns a promise, the result becomes Pending. Exceptions thrown by fn are not caught.

mapOr(defaultValue, fn)

mapOr<U>(defaultValue: U, fn: (value: T) => U | PromiseLike<U>): U | PromiseLike<U>;

Returns fn(value) on Ok, defaultValue on Err. Returns PromiseLike<U> when called on Pending or when fn is async.

mapOrElse(defaultFn, fn)

mapOrElse<U>(
defaultFn: (error: E) => U | PromiseLike<U>,
fn: (value: T) => U | PromiseLike<U>,
): U | PromiseLike<U>;

Like mapOr, but computes the default from the error.

Composition

andThen(fn)

andThen<U, F>(fn: (value: T) => Result<U, F>): Result<U, E | F>;

Runs fn on Ok, short-circuits on Err. The returned result inherits both error types. This is the monadic bind.

and(result)

and<U, F>(result: Result<U, F>): Result<U, E | F>;

Returns result when this is Ok, short-circuits on Err. No function, no lazy evaluation; result is always constructed up front.

or(result)

or<F>(result: Result<T, F>): Result<T, E | F>;

Returns this on Ok, result on Err. Use for unconditional fallback.

orElse(fn)

orElse<F>(fn: (error: E) => Result<T, F>): Result<T, F>;

Returns this on Ok, runs fn on Err. Use for recovery logic that should only execute on failure.

flatten()

flatten(): FlattenOk<T, E> | FlattenErr<T, E> | FlattenPending<T, E>;

Unwraps one level of nested Result. Ok(Ok(x)) becomes Ok(x), Ok(Err(e)) becomes Err(e). On Err or Pending, flatten preserves the outer shape.

Unwrapping

unwrap()

unwrap(): T | PromiseLike<T>;

Returns the value on Ok. Throws UnwrapError on Err. Returns a promise when called on Pending.

unwrapErr()

unwrapErr(): E | PromiseLike<E>;

Returns the error on Err. Throws UnwrapError on Ok. Returns a promise when called on Pending.

unwrapOr(value)

unwrapOr(value: T): T | PromiseLike<T>;

Returns the value on Ok, the provided default on Err. Returns a promise when called on Pending.

unwrapOrElse(fn)

unwrapOrElse(fn: (error: E) => T | PromiseLike<T>): T | PromiseLike<T>;

Returns the value on Ok, fn(error) on Err. Returns a promise when called on Pending or when fn is async.

Async boundary

settle()

settle(): PromiseLike<Settled<T, E>>;

Returns a promise that resolves to Ok or Err. On Ok and Err this resolves to this; on Pending it returns the inner promise.

See also

  • Ok, Err, Pending — per-shape behavior.
  • Result.do — fail-fast generator flow over these methods.
  • UnwrapError — thrown by unwrap / unwrapErr on the wrong shape.