Skip to content

Variable: all()

readonly all: <T>(results) => Result<OkTuple<T>, ErrUnion<T>>

Defined in: core/factories.ts:491

Combines multiple Results into a single Result containing tuple of values.

If all are Ok, returns Ok with array of all values. If any is Err, returns the first Err encountered (short-circuit).

Similar to Promise.all(), but for Results.

Type Parameters

T

T extends readonly Result<unknown, unknown>[]

Results tuple type

Parameters

results

T

Array of Results

Returns

Result<OkTuple<T>, ErrUnion<T>>

Ok with tuple of values or first Err

Examples

ts
// All Ok
const result = Result.all([
  Result.ok(1),
  Result.ok('two'),
  Result.ok(true)
])
result.unwrap() // [1, "two", true]
ts
// With Err - returns first error
const result = Result.all([
  Result.ok(1),
  Result.err('error 1'),
  Result.err('error 2')
])
// Err("error 1")
ts
// Validating multiple fields
const validated = Result.all([
  validateEmail(form.email),
  validatePassword(form.password),
  validateAge(form.age)
])

if (validated.isOk()) {
  const [email, password, age] = validated.unwrap()
  // All valid
}
ts
// Empty array
Result.all([]) // Ok([])