@mojotech/json-type-validation > "result"
The result of a computation that may fail. The decoding function Decoder.run returns a Result. The value of a Result is either Ok if the computation succeeded, or Err if there was some failure in the process.
▸ andThenA,B,E(f: function, r: Result<A, E>): Result<B, E>
Chain together a sequence of computations that may fail, similar to a Promise. If the first computation fails then the error will propagate through. If it succeeds, then f will be applied to the value, returning a new Result.
Type parameters:
Parameters:
| Param | Type |
|---|---|
| f | function |
| r | Result<A, E> |
Returns: Result<B, E>
▸ asPromiseV(r: Result<V, any>): Promise<V>
Create a Promise that either resolves with the result of Ok or rejects with the error of Err.
Type parameters:
Parameters:
| Param | Type |
|---|---|
| r | Result<V, any> |
Returns: Promise<V>
▸ errE(error: E): Err<E>
Wraps errors in an Err type.
Example: err('on fire') // => {ok: false, error: 'on fire'}
Type parameters:
Parameters:
| Param | Type |
|---|---|
| error | E |
Returns: Err<E>
▸ isErrE(r: Result<any, E>): boolean
Typeguard for Err.
Type parameters:
Parameters:
| Param | Type |
|---|---|
| r | Result<any, E> |
Returns: boolean
▸ isOkV(r: Result<V, any>): boolean
Typeguard for Ok.
Type parameters:
Parameters:
| Param | Type |
|---|---|
| r | Result<V, any> |
Returns: boolean
▸ mapA,B,E(f: function, r: Result<A, E>): Result<B, E>
Apply f to the result of an Ok, or pass the error through.
Type parameters:
Parameters:
| Param | Type |
|---|---|
| f | function |
| r | Result<A, E> |
Returns: Result<B, E>
▸ map2A,B,C,E(f: function, ar: Result<A, E>, br: Result<B, E>): Result<C, E>
Apply f to the result of two Oks, or pass an error through. If both Results are errors then the first one is returned.
Type parameters:
Parameters:
| Param | Type |
|---|---|
| f | function |
| ar | Result<A, E> |
| br | Result<B, E> |
Returns: Result<C, E>
▸ mapErrorV,A,B(f: function, r: Result<V, A>): Result<V, B>
Apply f to the error of an Err, or pass the success through.
Type parameters:
Parameters:
| Param | Type |
|---|---|
| f | function |
| r | Result<V, A> |
Returns: Result<V, B>
▸ okV(result: V): Ok<V>
Wraps values in an Ok type.
Example: ok(5) // => {ok: true, result: 5}
Type parameters:
Parameters:
| Param | Type |
|---|---|
| result | V |
Returns: Ok<V>
▸ successesA(results: Result<A, any>[]): A[]
Given an array of Results, return the successful values.
Type parameters:
Parameters:
| Param | Type |
|---|---|
| results | Result<A, any>[] |
Returns: A[]
▸ withDefaultV(defaultValue: V, r: Result<V, any>): V
Unwraps a Result and returns either the result of an Ok, or defaultValue.
Example:
Result.withDefault(5, number().run(json))
It would be nice if Decoder had an instance method that mirrored this function. Such a method would look something like this:
class Decoder<A> {
runWithDefault = (defaultValue: A, json: any): A =>
Result.withDefault(defaultValue, this.run(json));
}
number().runWithDefault(5, json)
Unfortunately, the type of defaultValue: A on the method causes issues with type inference on the object decoder in some situations. While these inference issues can be solved by providing the optional type argument for objects, the extra trouble and confusion doesn't seem worth it.
Type parameters:
Parameters:
| Param | Type |
|---|---|
| defaultValue | V |
| r | Result<V, any> |
Returns: V
▸ withExceptionV(r: Result<V, any>): V
Return the successful result, or throw an error.
Type parameters:
Parameters:
| Param | Type |
|---|---|
| r | Result<V, any> |
Returns: V