Skip to main content
Version: 1.x

@standardize/result

AKA Either Left/Right Operation Result(Kotlin)

Why?

Exceptions are commonplace in most codebases, this class allows "dangerous" operations to be wrapped in a Result<T, E> which encapsulate the possible errors or success states from a given method.

Typically the only time knowledge of an external package and its errors will be encoded is when the service is first written. By defining typed exceptions you encode information of possibly handleable errors you may otherwise feed directly to your users.

The async version of this monad AsyncResult<T, E> is best described as a promise with typed errors. The interface of the sync and async versions match to simplify access and allow method chaining.

Result<T, E> is not required in all of your code and should not be overused. It is best used at boundaries between services where retries or API changes are solid and infrequent.

typescript
const asyncResult = Result.invoke(getUser, userId)
 
asyncResult
const asyncResult: Result<Promise<User>, Error>
 
const result = Result.invoke(getUserSync, userId)
 
result
const result: Result<User, Error>

When correctly implemented it gives a great deal of control

typescript
const wrapped = AsyncResult.wrap(getUser)
const wrapped: (userId: string) => AsyncResult<User, Error>
const result =
const result: string | Error
await wrapped(userId)
.map(user => user.userId)
.tap(console.log)
.getEither()