Utilities & Combinators
Functions for combining, partitioning, and adapting values.
The library exposes several standalone utility functions in addition to the core catchers. These functions help you aggregate multiple Results or adapt external structures into Results.
Combinators help you manage arrays of Result objects.
Combines an array of Results into a single Result. If all succeed, it returns ok with an array of the data. If any Result is a failure, it immediately returns the first error encountered.
import { combine, ok, err } from '@catcherjs/core';
const combined = combine([ok(1), ok(2), ok(3)]);
console.log(combined.unwrap());
const withError = combine([ok(1), err('Failed'), ok(3)]);
console.log(withError.unwrapErr()); Similar to combine, but it does not short-circuit. It collects all errors.
import { combineAll, ok, err } from '@catcherjs/core';
const result = combineAll([ok(1), err('Err1'), err('Err2')]);
if (result.isErr()) {
console.log(result.error);
}Splits an array of Results into two separate arrays: one for the successful values and one for the errors. It never fails.
import { partition, ok, err } from '@catcherjs/core';
const { ok: data, err: errors } = partition([ok(1), err('Fail'), ok(3)]);
console.log(data);
console.log(errors); Adapters convert other programming patterns into the Result format.
Converts a nullable value (null or undefined) into a Result.
import { fromNullable } from '@catcherjs/core';
const user = null;
const result = fromNullable(user, new Error('User not found'));
console.log(result.isErr()); Wraps a synchronous function into one that returns a Result instead of throwing.
import { fromThrowable } from '@catcherjs/core';
const safeParse = fromThrowable(JSON.parse);
const result = safeParse('{ invalid }');
if (result.isErr()) {
console.error('Failed to parse:', result.error.message);
}An alias for catchError. It wraps a Promise and returns an AsyncResult.
import { fromPromise } from '@catcherjs/core';
const result = await fromPromise(fetch('https://api.example.com/data'));Component props
resultsResult<T, E>[]returnsResult<T[], E>Component props
resultsResult<T, E>[]returnsResult<T[], E[]>Component props
resultsResult<T, E>[]returns{ ok: T[], err: E[] }Component props
valueT | null | undefinederrorEreturnsResult<T, E>Component props
fn(...args) => TerrorsToCatchErrorClass[]returns(...args) => Result<T, E>Component props
promiseOrFnPromise<T> | (() => T | Promise<T>)errorsToCatchErrorClass[]returnsPromise<Result<T, E>>