@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.
typescriptconstasyncResult =Result .invoke (getUser ,userId )asyncResult constresult =Result .invoke (getUserSync ,userId )result
When correctly implemented it gives a great deal of control
typescriptconstwrapped =AsyncResult .wrap (getUser )constresult =awaitwrapped (userId ).map (user =>user .userId ).tap (console .log ).getEither ()