Skip to content

Variable: any()

readonly any: <T>(results) => Result<OkUnion<T>, ErrTuple<T>>

Defined in: core/factories.ts:617

Returns the first Ok Result, or all errors if none is Ok.

Scans the array until it finds an Ok (short-circuit). If no Ok is found, returns Err with array of all errors.

Similar to Promise.any().

Type Parameters

T

T extends readonly Result<unknown, unknown>[]

Results tuple type

Parameters

results

T

Array of Results

Returns

Result<OkUnion<T>, ErrTuple<T>>

First Ok or Err with all errors

Examples

ts
// First Ok
const result = Result.any([
  Result.err('error 1'),
  Result.ok(42),
  Result.ok(99)
])
result.unwrap() // 42
ts
// All Err
const result = Result.any([
  Result.err('error 1'),
  Result.err('error 2'),
  Result.err('error 3')
])
result.unwrapErr()
// ["error 1", "error 2", "error 3"]
ts
// Trying multiple data sources (fallback)
const data = Result.any([
  fetchFromCache(key),
  fetchFromDatabase(key),
  fetchFromAPI(key)
])
ts
// Empty array
Result.any([]) // Err([])