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

Combines multiple AsyncResults into a single AsyncResult 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 users = await AsyncResult.all([
  fetchUser("1"),
  fetchUser("2"),
  fetchUser("3")
]).take();

if (Result.isErr(users)) {
  console.error("Failed to fetch users");
} else {
  console.log(users); // [user1, user2, user3]
}

Type Parameters

The type of the Ok values

E extends BaseError

The type of the errors

Parameters

results: readonly (AsyncResult<T, E> | Promise<Result<T, E>>)[]

Array of AsyncResults or Promises to combine

Return Type

AsyncResult with array of values, or the first Err

Usage

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