Result extras
Result-specific operators and constructors for error handling, validation, and recovery.
These operators are available only on Result and help with error transformation, validation, and fallback chains.
Result-only operators
| Operator | Description | Usage |
|---|---|---|
mapErr | Transform the error channel | Err(e).mapErr(fn) → Err(newE) |
mapBoth | Transform both Ok and Err paths | .mapBoth(okFn, errFn) |
validate | Run validators, accumulate all errors | .validate([...validators]) |
Result.all | All must succeed, returns tuple | Result.all(r1, r2, ...) → Ok([...]) |
Result.any | First Ok wins, or all errors | Result.any(r1, r2, ...) → first Ok |
tapErr | Side effect for Err, returns self | Err(e).tapErr(fn) → Err(e) |
orElse | Recover from error with fallback | Err(e).orElse(fn) → fallback |
flip | Swap Ok and Err channels | Ok(v).flip() → Err(v) |
toOption | Convert to Option (discards error) | Ok(v).toOption() → Some(v) |
Constructors
| Operator | Description | Usage |
|---|---|---|
Result.fromNullable | Err for null/undefined | fromNullable(val, err) |
Result.fromPredicate | Create based on predicate | fromPredicate(val, pred, err) |
Result.tryCatch | Catch sync exceptions | tryCatch(fn, mapErr) |
Result.tryAsyncCatch | Catch async exceptions | tryAsyncCatch(fn, mapErr).toPromise() |
Notes
validatesupports sync and async validators; in async flows useawait ...toPromise().orElseis useful for fallback/recovery chains (try cache → try API → default).tap/tapErrare for side effects (logging, metrics) without altering the pipeline.