Skip to main content

Overview

Static methods on the Result namespace that combine lists of Results into a single Result. Similar to Promise.all(), but for Results. The combine variant short-circuits on the first error, while combineWithAllErrors collects all errors.

Result.combine()

Signature

Parameters

Result<T, E>[] | [Result<T1, E1>, ...]
required
An array or tuple of Results to combine. Can be homogeneous (all same type) or heterogeneous (different types).

Returns

  • If all Results are Ok, returns Ok containing an array/tuple of all Ok values
  • If any Result is Err, returns the first Err encountered (short-circuits)
  • Preserves tuple types for heterogeneous lists

Result.combineWithAllErrors()

Signature

Parameters

Result<T, E>[] | [Result<T1, E1>, ...]
required
An array or tuple of Results to combine.

Returns

  • If all Results are Ok, returns Ok containing an array/tuple of all Ok values
  • If any Results are Err, returns Err containing an array of all errors
  • Does not short-circuit - evaluates all Results

Examples

Basic combine - Homogeneous List

combine with Error (Short-circuits)

Heterogeneous List (Tuple)

combineWithAllErrors - Collects All Errors

Validation with All Errors

Parallel Operations

Array Processing

Type Preservation

Real-World Multi-Step Validation

With Different Error Types

Implementation Details

From the source code (result.ts:37-47):

When to Use Which?

Use combine when:

  • You want to fail fast on the first error
  • Errors are blocking (no point continuing)
  • You need best performance (short-circuits)
  • Similar to && operator behavior

Use combineWithAllErrors when:

  • You want to collect all errors
  • Useful for form validation (show all errors at once)
  • Non-blocking errors (all checks should run)
  • Better user experience (see all issues)