result
Represents a success/failure outcome without using exceptions. Pattern commonly used in functional programming.
Import
import type { Result } from '@fullstacksjs/toolbox';Signature
type Result<Ok, Err> =
| { ok: true; value: Ok }
| { ok: false; error: Err }Examples
const success: Result<number, string> = { ok: true, value: 42 };
const failure: Result<number, string> = { ok: false, error: 'Something went wrong' };AsyncResult
A Promise wrapper around a Result type for async operations. Use for cleaner error handling with await.
Import
import type { AsyncResult } from '@fullstacksjs/toolbox';Signature
type AsyncResult<Ok, Err> = Promise<Result<Ok, Err>>;Example
const asyncRes: AsyncResult<number, string> = Promise.resolve({ ok: true, value: 42 });Ok
Helper function to create a Result in the success state.
Import
import { Ok } from '@fullstacksjs/toolbox';Signature
function Ok<T>(x: T): Result<T, never>Example
const success = Ok(42); // { ok: true, value: 42 }Err
Helper function to create a Result in the error state.
Import
import { Err } from '@fullstacksjs/toolbox';Signature
function Err<T>(x: T): Result<never, T>Example
const failure = Err('Something went wrong'); // { ok: false, error: 'Something went wrong' }