carbonteq /FP

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

OperatorDescriptionUsage
mapErrTransform the error channel
Err(e).mapErr(fn) → Err(newE)
mapBothTransform both Ok and Err paths
.mapBoth(okFn, errFn)
validateRun validators, accumulate all errors
.validate([...validators])
Result.allAll must succeed, returns tuple
Result.all(r1, r2, ...) → Ok([...])
Result.anyFirst Ok wins, or all errors
Result.any(r1, r2, ...) → first Ok
tapErrSide effect for Err, returns self
Err(e).tapErr(fn) → Err(e)
orElseRecover from error with fallback
Err(e).orElse(fn) → fallback
flipSwap Ok and Err channels
Ok(v).flip() → Err(v)
toOptionConvert to Option (discards error)
Ok(v).toOption() → Some(v)

Constructors

OperatorDescriptionUsage
Result.fromNullableErr for null/undefined
fromNullable(val, err)
Result.fromPredicateCreate based on predicate
fromPredicate(val, pred, err)
Result.tryCatchCatch sync exceptions
tryCatch(fn, mapErr)
Result.tryAsyncCatchCatch async exceptions
tryAsyncCatch(fn, mapErr).toPromise()

Notes

  • validate supports sync and async validators; in async flows use await ...toPromise().
  • orElse is useful for fallback/recovery chains (try cache → try API → default).
  • tap/tapErr are for side effects (logging, metrics) without altering the pipeline.

On this page