Skip to main content
Module

x/eitherway/mod.ts>Results.any

Yet Another Option and Result Implementation - providing safe abstractions for fallible flows inspired by F# and Rust
Latest
function Results.any
import { Results } from "https://deno.land/x/eitherway@0.10.0/mod.ts";
const { any } = Results;

Use this to obtain the first found Ok<T> from an Array<Result<T,E>> or Iterable<Result<T,E>>. If no Ok<T> value is found, the Err<E> values are collected into an array and returned.

This function also works on variadic tuples and preserves the individual types of the tuple members.

Examples

Example 1

import { assert } from "./assert.ts";
import { Result, Results } from "./mod.ts";

const str = "thing" as string | TypeError;
const num = 5 as number | RangeError;
const bool = true as boolean | ReferenceError;

const tuple = [ Result(str), Result(num), Result(bool) ] as const;

const res: Result<
  string | number | boolean,
  readonly [TypeError, RangeError, ReferenceError]
> = Results.any(tuple);

assert(res.isOk());

Type Parameters

R extends Readonly<ArrayLike<Result<unknown, unknown>>>

Parameters

results: R

Returns

Result<InferredOkUnion<R>, InferredErrTuple<R>>

Parameters

results: Readonly<Iterable<Result<T, E>>>