method Result.any
Result.any<T, E extends BaseError>(results: readonly Result<T, E>[]): Result<T, E>

Returns the first Ok result from an array of Results.

If any Result is Ok, returns that Ok result. If all Results are Err, returns the last Err.

Examples

Example 1

const results = [Result.err(new Error("e1")), Result.ok(2), Result.ok(3)];
const first = Result.any(results);
// Ok(2)

const allErrors = [
  Result.err(new Error("e1")),
  Result.err(new Error("e2")),
  Result.err(new Error("e3"))
];
const first2 = Result.any(allErrors);
// Err(Error("e3")) - the last error

Type Parameters

The type of the Ok values

E extends BaseError

The type of the errors

Parameters

results: readonly Result<T, E>[]

Array of Results to check

Return Type

The first Ok, or the last Err if all failed

Usage

import { Result } from "result/mod.ts";