Skip to content

Latest commit

 

History

History
159 lines (107 loc) · 2.02 KB

File metadata and controls

159 lines (107 loc) · 2.02 KB

AsyncRepositoryReturn

Overview

AsyncRepositoryReturn<T> is the asynchronous counterpart of RepositoryReturn<T>.

It models repository-like async operations where:

  • absence of data is meaningful
  • errors are explicit
  • results may contain multiple values
AsyncRepositoryReturn<T>
   Promise<RepositoryReturn<T>>
   Promise<Either<Error, Maybe<Array<T>>>>

Why This Exists

Using Promise<T | null> or throwing exceptions conflates:

  • transport errors
  • absence of data
  • empty collections

AsyncRepositoryReturn keeps these concerns orthogonal and composable.


Construction

ARR.success(value)
ARR.failure(error)
ARR.none()

ARR.create(value)
ARR.fromPromise(promise)
ARR.fromResult(result)

State Inspection

await arr.isSuccess()
await arr.isFailure()
await arr.isNone()

Pattern Matching

arr.match(
  onNone,
  onFailure,
  onSuccess
)

Each branch:

  • may return values, RepositoryReturn, or Promises
  • exceptions are captured as failures

Mapping

Success

arr.map(fn)
arr.mapSuccess(fn)
arr.mapElements(fn)
arr.mapFirst(fn)

Failure / None

arr.mapFailure(fn)
arr.mapNone(fn)

Transformations

arr.noneAsFailure(err)
arr.noneAsSuccess(values)
arr.failureAsNone()

Monad Operations

arr.then(fn)
arr._then(fn)

Behaves like Promise.then, but:

  • preserves semantic structure
  • never loses error / none distinction

Interop

Convert to Result

await arr.asResult(fallback)
await arr.asFirstElementResult(fallback)

Extract

await arr.toPromise()
await arr.value

Error Handling

arr.catch(err => ...)

Errors become structured failures, not thrown exceptions.


  • Referential transparency
  • Exception safety
  • Explicit absence
  • Total pattern matching
  • Async-safe monadic composition

Mental Model

RepositoryReturn describes “what happened” AsyncRepositoryReturn describes “what will happen”