Collections
Working with multiple Results at once.
Overview
Result.js provides six collection methods for combining, filtering, and partitioning arrays of Results:
| Method | Returns on success | Returns on failure |
|---|---|---|
all() | Ok with tuple of values | First Err encountered |
allSettled() | Always Ok with status array | Never fails |
any() | First Ok found | Err with all errors |
partition() | [values[], errors[]] | Never fails |
values() | T[] (Ok values only) | Never fails |
errors() | E[] (Err values only) | Never fails |
all()
Combines multiple Results into a single Result containing a tuple of values. Short-circuits on the first Err.
// All succeed — returns tuple
const result = Result.all([
Result.ok(1),
Result.ok('two'),
Result.ok(true)
])
result.unwrap() // [1, 'two', true]// First error stops execution
const result = Result.all([
Result.ok(1),
Result.err('error A'),
Result.err('error B')
])
result.unwrapErr() // 'error A'Use for: validation pipelines where all steps must succeed.
const validated = Result.all([
validateEmail(form.email),
validatePassword(form.password),
validateAge(form.age)
])
validated.match({
ok: ([email, password, age]) => register({ email, password, age }),
err: (error) => showError(error)
})allSettled()
Collects the status of all Results without failing. Similar to Promise.allSettled(). Always returns Ok with an array of status objects.
const result = Result.allSettled([
Result.ok(1),
Result.err('failed'),
Result.ok(3)
])
const settled = result.unwrap()
// [
// { status: 'ok', value: 1 },
// { status: 'err', reason: 'failed' },
// { status: 'ok', value: 3 }
// ]const successes = settled.filter(r => r.status === 'ok')
const failures = settled.filter(r => r.status === 'err')Use for: batch processing where you need a report of all outcomes.
const results = await Promise.all(
records.map(record => processRecord(record))
)
const settled = Result.allSettled(results).unwrap()
console.log(`Processed: ${settled.length}`)
console.log(`Success: ${settled.filter(r => r.status === 'ok').length}`)
console.log(`Failed: ${settled.filter(r => r.status === 'err').length}`)any()
Returns the first Ok found, or an Err containing all errors if none succeed. Similar to Promise.any().
// Returns first Ok
const result = Result.any([
Result.err('error A'),
Result.ok(42),
Result.ok(99)
])
result.unwrap() // 42// All fail — returns all errors
const result = Result.any([
Result.err('error A'),
Result.err('error B'),
Result.err('error C')
])
result.unwrapErr() // ['error A', 'error B', 'error C']Use for: trying multiple independent sources and succeeding with the first one.
// Try multiple servers in parallel, use first response
const data = Result.any([
fetchFromServer('us-east'),
fetchFromServer('eu-west'),
fetchFromServer('ap-south')
])
data.match({
ok: (response) => process(response),
err: (errors) => handleAllFailed(errors)
})partition()
Separates an array of Results into two arrays: successes and failures.
const [oks, errs] = Result.partition([
Result.ok(1),
Result.err('failure A'),
Result.ok(2),
Result.err('failure B')
])
// oks → [1, 2]
// errs → ['failure A', 'failure B']Use for: batch processing where you want to handle successes and failures separately.
const results = records.map(record => processRecord(record))
const [processed, failed] = Result.partition(results)
await saveAll(processed)
await reportFailures(failed)values()
Extracts only the success values from an array of Results, discarding errors.
const vals = Result.values([
Result.ok(1),
Result.err('fail'),
Result.ok(2)
])
// [1, 2]Use for: when you only care about successful results and want to silently ignore failures.
const users = Result.values(
ids.map(id => findUser(id))
)
// Only resolved users, failed lookups ignorederrors()
Extracts only the errors from an array of Results, discarding successes.
const errs = Result.errors([
Result.ok(1),
Result.err('fail A'),
Result.ok(2),
Result.err('fail B')
])
// ['fail A', 'fail B']Use for: collecting all failures for logging or reporting.
const results = records.map(record => processRecord(record))
const failures = Result.errors(results)
if (failures.length > 0) {
logger.warn('Some records failed', { failures })
}Choosing the Right Method
Need ALL to succeed? → all()
Need a report of ALL? → allSettled()
Need the FIRST to succeed? → any()
Need both lists separately? → partition()
Need only successes? → values()
Need only failures? → errors()Next Steps
- Operation Chaining — Compose sequential operations
- Error Handling — Recovery strategies