Overview
Static methods on the Result namespace that combine lists of Results into a single Result. Similar toPromise.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, returnsOkcontaining an array/tuple of all Ok values - If any Result is
Err, returns the firstErrencountered (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, returnsOkcontaining an array/tuple of all Ok values - If any Results are
Err, returnsErrcontaining 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)
Related
- ResultAsync.combine() - Async version
- ResultAsync.combineWithAllErrors() - Async version with all errors
- Result.andThen() - Sequential combining