Class: Fail<E>
Type parameters
| Name | Type |
|---|---|
E | extends Error |
Hierarchy
Result<unknown,E>↳
Fail
Properties
failAsync
▪ Static failAsync: <T, F>(error: F) => AsyncResult<T, F> = AsyncResult.fail
Type declaration
▸ <T, F>(error): AsyncResult<T, F>
Returns an AsyncResult that has failed. Useful for testing.
Example
tsconst maybeUser = await AsyncResult.fail<User>(new Error('User not found'))
Type parameters
| Name | Type |
|---|---|
T | unknown |
F | extends Error = Error |
Parameters
| Name | Type |
|---|---|
error | F |
Returns
AsyncResult<T, F>
Inherited from
Defined in
packages/result/src/Result.ts:83
fromPromise
▪ Static fromPromise: <T, F>(promise: Promise<T>) => AsyncResult<T, F> = AsyncResult.fromPromise
Type declaration
▸ <T, F>(promise): AsyncResult<T, F>
Create an AsyncResult from a pending Promise
Example
tsResult.fromPromise(fetch('https://example.com')).map(...)
Type parameters
| Name | Type |
|---|---|
T | T |
F | extends Error = Error |
Parameters
| Name | Type |
|---|---|
promise | Promise<T> |
Returns
AsyncResult<T, F>
Inherited from
Defined in
packages/result/src/Result.ts:53
fromPromiseAsync
▪ Static fromPromiseAsync: <T, F>(promise: Promise<T>) => AsyncResult<T, F> = AsyncResult.fromPromise
Type declaration
▸ <T, F>(promise): AsyncResult<T, F>
Returns an AsyncResult that will resolve to the given value or rejected error.
Example
tsconst maybeUser = await AsyncResult.of<User>(getUser(userId))
Type parameters
| Name | Type |
|---|---|
T | T |
F | extends Error = Error |
Parameters
| Name | Type |
|---|---|
promise | Promise<T> |
Returns
AsyncResult<T, F>
Inherited from
Defined in
packages/result/src/Result.ts:55
invokeAsync
▪ Static invokeAsync: <FN>(fn: FN, ...args: Parameters<FN>) => AsyncResult<ReturnType<FN>, Error> = AsyncResult.invoke
Type declaration
▸ <FN>(fn, ...args): AsyncResult<ReturnType<FN>, Error>
Invoke a function that returns a promise and wrap the result in an AsyncResult.
If the function throws synchronously or asynchronously, the error will be wrapped in an AsyncResult.
Example
tsconst maybeUser = await AsyncResult.invoke(getUser, userId) // AsyncResult<User, Error>
Type parameters
| Name | Type |
|---|---|
FN | extends (...args: any[]) => Promise<any> |
Parameters
| Name | Type |
|---|---|
fn | FN |
...args | Parameters<FN> |
Returns
AsyncResult<ReturnType<FN>, Error>
Inherited from
Defined in
packages/result/src/Result.ts:24
successAsync
▪ Static successAsync: <T, F>(value: T) => AsyncResult<T, F> = AsyncResult.success
Type declaration
▸ <T, F>(value): AsyncResult<T, F>
Returns an AsyncResult that has succeeded. Useful for testing.
Example
tsconst maybeUser = await AsyncResult.success<User>({ id: 1, name: 'John' })
Type parameters
| Name | Type |
|---|---|
T | T |
F | extends Error = Error |
Parameters
| Name | Type |
|---|---|
value | T |
Returns
AsyncResult<T, F>
Inherited from
Defined in
packages/result/src/Result.ts:69
wrapAsync
▪ Static wrapAsync: <FN>(fn: FN) => (...args: Parameters<FN>) => FN extends (...args: any[]) => Promise<RT> ? AsyncResult<RT, Error> : never = AsyncResult.wrap
Type declaration
▸ <FN>(fn): (...args: Parameters<FN>) => FN extends (...args: any[]) => Promise<RT> ? AsyncResult<RT, Error> : never
Wrap a function that returns a promise in an AsyncResult.
Example
tsconst safeGetUser = AsyncResult.wrap(getUser)const maybeUser = await safeGetUser(userId) // AsyncResult<User, Error>
Type parameters
| Name | Type |
|---|---|
FN | extends (...args: any[]) => any |
Parameters
| Name | Type |
|---|---|
fn | FN |
Returns
fn
▸ (...args): FN extends (...args: any[]) => Promise<RT> ? AsyncResult<RT, Error> : never
Parameters
| Name | Type |
|---|---|
...args | Parameters<FN> |
Returns
FN extends (...args: any[]) => Promise<RT> ? AsyncResult<RT, Error> : never
Inherited from
Defined in
packages/result/src/Result.ts:43
Methods
flatMap
▸ flatMap<X, XF>(mapSuccess, mapError?): Result<X, XF>
Identical to '.map' but expects a Result to be returned from the mapping function.
Example
tsconst validate = Result.wrap(validator)const parse = Result.wrap(JSON.parse)parse('{}').flatMap(validate) // Result<Parsed, Error>#### Type parameters| Name | Type || :------ | :------ || `X` | `X` || `XF` | extends `Error` |#### Parameters| Name | Type || :------ | :------ || `mapSuccess` | (`success`: `unknown`) => [`Result`](Result.md)<`X`, `Error`\> || `mapError?` | (`error`: `E`) => [`Result`](Result.md)<`XF`, `Error`\> |#### Returns[`Result`](Result.md)<`X`, `XF`\>#### Inherited from[Result](Result.md).[flatMap](Result.md#flatmap)#### Defined in[packages/result/src/Result.ts:255](https://github.com/jamesapple/standardize/blob/c5eecb2/packages/result/src/Result.ts#L255)___### flatMapAsync▸ **flatMapAsync**<`X`, `XF`\>(`mapSuccess`, `mapError?`): [`AsyncResult`](AsyncResult.md)<`X`, `E` \| `XF`\>Identical to '.mapAsync' but expects a Result to be returned from themapping function.**`Example`**```tsconst parse = Result.wrapAsync(JSON.parse)const getUser = parse('{}').map(data => data.userId).flatMapAsync(requireUserById) // AsyncResult<User, Error>#### Type parameters| Name | Type || :------ | :------ || `X` | `X` || `XF` | extends `Error` = `E` |#### Parameters| Name | Type || :------ | :------ || `mapSuccess` | (`success`: `unknown`) => [`AsyncResult`](/docs/typedoc/result/classes/AsyncResult)<`X`, `Error`\> || `mapError?` | (`error`: `E`) => [`AsyncResult`](/docs/typedoc/result/classes/AsyncResult)<`XF`, `Error`\> |#### Returns[`AsyncResult`](/docs/typedoc/result/classes/AsyncResult)<`X`, `E` \| `XF`\>#### Inherited from[Result](/docs/typedoc/result/classes/Result).[flatMapAsync](/docs/typedoc/result/classes/Result#flatmapasync)#### Defined in[packages/result/src/Result.ts:275](https://github.com/jamesapple/standardize/blob/c5eecb2/packages/result/src/Result.ts#L275)___### get▸ **get**(): `unknown`Directly access the success value. If the result is a failure, the errorvalue is thrown.**`Example`**```tsResult.invoke(JSON.parse, '{}').get() // {}Result.invoke(JSON.parse, '{').get() // throwsResult.invoke(JSON.parse, '{').get({}) // {}
Returns
unknown
Inherited from
Defined in
packages/result/src/Result.ts:343
getEither
▸ getEither(): E
Returns the success value or the error value if the result is a failure.
Example
tsResult.invoke(JSON.parse, '{').getEither() // SyntaxErrorResult.invoke(JSON.parse, '{}').getEither() // {}
Returns
E
Overrides
Defined in
packages/result/src/Result.ts:424
isFail
▸ isFail(): this is Fail<E>
Checks if this result is a failure.
Should only be used as an escape hatch for interop with legacy code.
Example
tsconst result = Result.invoke(JSON.parse, '{')if (result.isFail()) {result.get() // SyntaxError}#### Returnsthis is Fail<E\>#### Overrides[Result](Result.md).[isFail](Result.md#isfail)#### Defined in[packages/result/src/Result.ts:432](https://github.com/jamesapple/standardize/blob/c5eecb2/packages/result/src/Result.ts#L432)___### isSuccess▸ **isSuccess**(): ``false``Checks if this result is a success. This should be checked before calling`.get()`.Should only be used as an escape hatch for interop with legacy code.**`Example`**```tsconst result = Result.invoke(JSON.parse, '{}')if (result.isSuccess()) {result.get() // {}}#### Returns``false``#### Overrides[Result](/docs/typedoc/result/classes/Result).[isSuccess](/docs/typedoc/result/classes/Result#issuccess)#### Defined in[packages/result/src/Result.ts:428](https://github.com/jamesapple/standardize/blob/c5eecb2/packages/result/src/Result.ts#L428)___### map▸ **map**<`X`, `Y`\>(`_`, `mapError?`): [`Result`](/docs/typedoc/result/classes/Result)<`X`, `Y`\>Transform the data in a Result by applying a function to a containedsuccess value, leaving an error value untouched or optionally changing itas well.**`Example`**```tsResult.success('foo').map((value) => value.toUpperCase()) // Result<'FOO'>
Type parameters
| Name | Type |
|---|---|
X | X |
Y | extends Error = E |
Parameters
| Name | Type |
|---|---|
_ | (success: unknown) => X |
mapError? | (error: E) => Y |
Returns
Result<X, Y>
Overrides
Defined in
packages/result/src/Result.ts:398
mapAsync
▸ mapAsync<X, Y>(_, mapError?): AsyncResult<X, Y>
Transform the data in a Result by applying a function to a contained success value asynchronously, leaving an error value untouched or optionally changing it as well.
Example
tsResult.success('userId').mapAsync(getUser) // AsyncResult<User>
Type parameters
| Name | Type |
|---|---|
X | X |
Y | extends Error = E |
Parameters
| Name | Type |
|---|---|
_ | (success: unknown) => Promise<X> |
mapError? | (error: E) => Promise<Y> |
Returns
AsyncResult<X, Y>
Overrides
Defined in
packages/result/src/Result.ts:411
mapFailure
▸ mapFailure<X>(mapFailure): Result<unknown, X>
Transform the failure value by applying a function to it.
Example
tsResult.invoke(JSON.parse, '{').mapFailure((error) => new ParsingError(error.message)) // Result<unknown, string>
Type parameters
| Name | Type |
|---|---|
X | extends Error |
Parameters
| Name | Type |
|---|---|
mapFailure | (failed: E) => X |
Returns
Result<unknown, X>
Inherited from
Defined in
packages/result/src/Result.ts:165
mapFailureAsync
▸ mapFailureAsync<X>(mapFailure): AsyncResult<unknown, X>
Transform the failure value by applying a function to it asynchronously.
Example
tsResult.invoke(JSON.parse, '{').mapFailureAsync(enrichErrorAsync) // AsyncResult<any, ParsingError>#### Type parameters| Name | Type || :------ | :------ || `X` | extends `Error` |#### Parameters| Name | Type || :------ | :------ || `mapFailure` | (`failed`: `E`) => `Promise`<`X`\> |#### Returns[`AsyncResult`](AsyncResult.md)<`unknown`, `X`\>#### Inherited from[Result](Result.md).[mapFailureAsync](Result.md#mapfailureasync)#### Defined in[packages/result/src/Result.ts:177](https://github.com/jamesapple/standardize/blob/c5eecb2/packages/result/src/Result.ts#L177)___### recover▸ **recover**<`R`, `FF`\>(`mapError`): [`Result`](Result.md)<`unknown`, `FF`\>Attempt to recover from a failure by mapping the error value to a successvalue. If the result is a success, the mapping function is ignored.If the result is a failure, the mapping function is invoked and theresult is returned. If the mapping function throws an error, the resultwill be a failure.**`Example`**```tsResult.invoke(JSON.parse, '{').recover((error) => ({})) // Result<{}, Error>#### Type parameters| Name | Type || :------ | :------ || `R` | `R` || `FF` | extends `Error` = `Error` |#### Parameters| Name | Type || :------ | :------ || `mapError` | (`err`: `E`) => `R` |#### Returns[`Result`](/docs/typedoc/result/classes/Result)<`unknown`, `FF`\>#### Inherited from[Result](/docs/typedoc/result/classes/Result).[recover](/docs/typedoc/result/classes/Result#recover)#### Defined in[packages/result/src/Result.ts:301](https://github.com/jamesapple/standardize/blob/c5eecb2/packages/result/src/Result.ts#L301)___### recoverAsync▸ **recoverAsync**<`R`\>(`mapError`): [`AsyncResult`](/docs/typedoc/result/classes/AsyncResult)<`unknown`, `Error`\>Attempt to recover from a failure by mapping the error value to a successvalue. If the result is a success, the mapping function is ignored.If the result is a failure, the mapping function is invoked and theresult is returned. If the mapping function throws an error, the resultwill be a failure.**`Example`**```tsawait Result.invoke(JSON.parse, '{').recoverAsync(loadDefaultData) // AsyncResult<DefaultData, Error>
Type parameters
| Name |
|---|
R |
Parameters
| Name | Type |
|---|---|
mapError | (err: E) => Promise<R> |
Returns
AsyncResult<unknown, Error>
Inherited from
Defined in
packages/result/src/Result.ts:325
tap
▸ tap(tapSuccess, tapError?): Result<unknown, E>
Access either the success or error value without changing the type. This is useful for chaining together a sequence of computations where you want to insert logging, validation or any other side effect.
If these functions throw an error, the result will be a failure.
Example
tsResult.invoke(JSON.parse, '{}').tap(console.log, console.error).map(...)
Parameters
| Name | Type |
|---|---|
tapSuccess | (success: unknown) => unknown |
tapError? | (error: E) => unknown |
Returns
Result<unknown, E>
Inherited from
Defined in
packages/result/src/Result.ts:197
tapAsync
▸ tapAsync(tapSuccess, tapError?): AsyncResult<unknown, E>
Access either the success or error value asynchronously without changing the type. This is useful for chaining together a sequence of computations where you want to insert logging, validation or any other side effect.
If these functions throw an error, the result will be a failure.
Example
tsawait Result.invoke(JSON.parse, '{}').tapAsync(console.log, console.error).map(...)
Parameters
| Name | Type |
|---|---|
tapSuccess | (success: unknown) => Promise<unknown> |
tapError? | (error: E) => Promise<unknown> |
Returns
AsyncResult<unknown, E>
Inherited from
Defined in
packages/result/src/Result.ts:228
fail
▸ Static fail<T, F>(value): Result<T, F>
Create a failed result. Useful for testing
Example
tsResult.fail(new Error('foo')).map(...)
Type parameters
| Name | Type |
|---|---|
T | unknown |
F | extends Error = Error |
Parameters
| Name | Type |
|---|---|
value | F |
Returns
Result<T, F>
Inherited from
Defined in
packages/result/src/Result.ts:79
invoke
▸ Static invoke<FN>(throwable, ...args): Result<ReturnType<FN>, Error>
Immediately invoke an unsafe function and return a result type
Example
tsResult.invoke(JSON.parse, '{}').map(...)
Type parameters
| Name | Type |
|---|---|
FN | extends (...args: any[]) => any |
Parameters
| Name | Type |
|---|---|
throwable | FN |
...args | Parameters<FN> |
Returns
Result<ReturnType<FN>, Error>
Inherited from
Defined in
packages/result/src/Result.ts:13
of
▸ Static of<E>(value): Fail<E>
Type parameters
| Name | Type |
|---|---|
E | extends Error |
Parameters
| Name | Type |
|---|---|
value | E |
Returns
Fail<E>
Defined in
packages/result/src/Result.ts:391
success
▸ Static success<T, F>(value): Result<T, F>
Create a successful result. Useful for testing
Example
tsResult.success('foo').map(...)
Type parameters
| Name | Type |
|---|---|
T | T |
F | extends Error = Error |
Parameters
| Name | Type |
|---|---|
value | T |
Returns
Result<T, F>
Inherited from
Defined in
packages/result/src/Result.ts:65
wrap
▸ Static wrap<FN>(fn): (...args: Parameters<FN>) => FN extends (...args: any[]) => RT ? Result<RT, Error> : never
Wraps a potentially unsafe function and makes it return a result type
Example
tsconst safeParse = Result.wrap(JSON.parse)safeParse('{}').map(...)
Type parameters
| Name | Type |
|---|---|
FN | extends (...args: any[]) => any |
Parameters
| Name | Type |
|---|---|
fn | FN |
Returns
fn
▸ (...args): FN extends (...args: any[]) => RT ? Result<RT, Error> : never
Parameters
| Name | Type |
|---|---|
...args | Parameters<FN> |
Returns
FN extends (...args: any[]) => RT ? Result<RT, Error> : never