allfunctionfunction all<T, E>(results: readonly Result<T, E>[]): Result<readonly T[], E>Every result's value in order, or the first failure.
Errors as values: a step that can fail returns a Result whose error is a type of its own, usually a union told apart by kind (0001-core FR-001). Taken from erga's kernel and the machine setup's result.ts, which wrote the same thing twice.
import { andThen, err, ok, type Result } from "./result.ts";
const half = (n: number): Result<number, string> => n % 2 === 0 ? ok(n / 2) : err(`${n} is odd`);
andThen(half(8), half); // { kind: "ok", value: 2 }allfunctionfunction all<T, E>(results: readonly Result<T, E>[]): Result<readonly T[], E>Every result's value in order, or the first failure.
andThenfunctionfunction andThen<T, U, E>(result: Result<T, E>, f: (value: T) => Result<U, E>): Result<U, E>A second step run on the value of a success; a failure unchanged.
attemptfunctionfunction attempt<T, E>(f: () => T, onThrow: (thrown: unknown) => E): Result<T, E>What a function that throws returns, as a result: its value, or what it threw turned into an error by onThrow.
doneconstconst done: Ok<void>The success that carries nothing: a step that only had to happen.
errfunctionfunction err<E>(error: E): Err<E>A failure.
isErrfunctionfunction isErr<T, E>(result: Result<T, E>): result is Err<E>Whether a result is a failure (a type guard).
isOkfunctionfunction isOk<T, E>(result: Result<T, E>): result is Ok<T>Whether a result is a success (a type guard).
mapfunctionfunction map<T, U, E>(result: Result<T, E>, f: (value: T) => U): Result<U, E>The value of a success transformed; a failure unchanged.
mapErrfunctionfunction mapErr<T, E, F>(result: Result<T, E>, f: (error: E) => F): Result<T, F>The error of a failure transformed; a success unchanged.
matchfunctionfunction match<T, E, R>(result: Result<T, E>, on: { readonly ok: (value: T) => R; readonly err: (error: E) => R; }): ROne function for each alternative, and what the one that applies returns.
okfunctionfunction ok<T>(value: T): Ok<T>A success.
orElsefunctionfunction orElse<T, E, F>(result: Result<T, E>, f: (error: E) => Result<T, F>): Result<T, F>A second step run on the error of a failure, which may recover; a success unchanged.
settlefunctionfunction settle<T, E>(promise: PromiseLike<T>, onReject: (reason: unknown) => E): Promise<Result<T, E>>What a promise settles to, as a result: its value, or its rejection turned into an error by onReject.
unwrapOrfunctionfunction unwrapOr<T, E>(result: Result<T, E>, fallback: T): TThe value of a success, or fallback for a failure.
Errtypeno test imports ittype Err<E> = { readonly kind: "err"; readonly error: E };A step's failure, with its typed error.
Oktypeno test imports ittype Ok<T> = { readonly kind: "ok"; readonly value: T };A step's success, with its value.
Resulttypetype Result<T, E> = Ok<T> | Err<E>;What a step that can fail returns.