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

Combines multiple Results into a single Result containing an array.

If all Results are Ok, returns Ok with an array of all values. If any Result is Err, returns the first Err encountered.

Examples

Example 1

const results = [Result.ok(1), Result.ok(2), Result.ok(3)];
const combined = Result.all(results);
// Ok([1, 2, 3])

const withError = [Result.ok(1), Result.err(new ValidationError("Failed")), Result.ok(3)];
const combined2 = Result.all(withError);
// Err(ValidationError)

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 combine

Return Type

Result<T[], E>

Ok with array of values, or the first Err

Usage

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