Skip to main content
Version: 1.x

Class: Success<T>

Type parameters

Name
T

Hierarchy

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

ts
const maybeUser = await AsyncResult.fail<User>(new Error('User not found'))
Type parameters
NameType
Tunknown
Fextends Error = Error
Parameters
NameType
errorF
Returns

AsyncResult<T, F>

Inherited from

Result.failAsync

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

ts
Result.fromPromise(fetch('https://example.com')).map(...)
Type parameters
NameType
TT
Fextends Error = Error
Parameters
NameType
promisePromise<T>
Returns

AsyncResult<T, F>

Inherited from

Result.fromPromise

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

ts
const maybeUser = await AsyncResult.of<User>(getUser(userId))
Type parameters
NameType
TT
Fextends Error = Error
Parameters
NameType
promisePromise<T>
Returns

AsyncResult<T, F>

Inherited from

Result.fromPromiseAsync

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

ts
const maybeUser = await AsyncResult.invoke(getUser, userId) // AsyncResult<User, Error>
Type parameters
NameType
FNextends (...args: any[]) => Promise<any>
Parameters
NameType
fnFN
...argsParameters<FN>
Returns

AsyncResult<ReturnType<FN>, Error>

Inherited from

Result.invokeAsync

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

ts
const maybeUser = await AsyncResult.success<User>({ id: 1, name: 'John' })
Type parameters
NameType
TT
Fextends Error = Error
Parameters
NameType
valueT
Returns

AsyncResult<T, F>

Inherited from

Result.successAsync

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

ts
const safeGetUser = AsyncResult.wrap(getUser)
const maybeUser = await safeGetUser(userId) // AsyncResult<User, Error>
Type parameters
NameType
FNextends (...args: any[]) => any
Parameters
NameType
fnFN
Returns

fn

▸ (...args): FN extends (...args: any[]) => Promise<RT> ? AsyncResult<RT, Error> : never

Parameters
NameType
...argsParameters<FN>
Returns

FN extends (...args: any[]) => Promise<RT> ? AsyncResult<RT, Error> : never

Inherited from

Result.wrapAsync

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

ts
const 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`: `T`) => [`Result`](Result.md)<`X`, `Error`\> |
| `mapError?` | (`error`: `Error`) => [`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`, `Error` \| `XF`\>
Identical to '.mapAsync' but expects a Result to be returned from the
mapping function.
**`Example`**
```ts
const 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` = `Error` |
#### Parameters
| Name | Type |
| :------ | :------ |
| `mapSuccess` | (`success`: `T`) => [`AsyncResult`](/docs/typedoc/result/classes/AsyncResult)<`X`, `Error`\> |
| `mapError?` | (`error`: `Error`) => [`AsyncResult`](/docs/typedoc/result/classes/AsyncResult)<`XF`, `Error`\> |
#### Returns
[`AsyncResult`](/docs/typedoc/result/classes/AsyncResult)<`X`, `Error` \| `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**(): `T`
Directly access the success value. If the result is a failure, the error
value is thrown.
**`Example`**
```ts
Result.invoke(JSON.parse, '{}').get() // {}
Result.invoke(JSON.parse, '{').get() // throws
Result.invoke(JSON.parse, '{').get({}) // {}

Returns

T

Inherited from

Result.get

Defined in

packages/result/src/Result.ts:343


getEither

getEither(): T

Returns the success value or the error value if the result is a failure.

Example

ts
Result.invoke(JSON.parse, '{').getEither() // SyntaxError
Result.invoke(JSON.parse, '{}').getEither() // {}

Returns

T

Overrides

Result.getEither

Defined in

packages/result/src/Result.ts:377


isFail

isFail(): this is Fail<Error>

Checks if this result is a failure.

Should only be used as an escape hatch for interop with legacy code.

Example

ts
const result = Result.invoke(JSON.parse, '{')
if (result.isFail()) {
result.get() // SyntaxError
}
#### Returns
this is Fail<Error\>
#### Overrides
[Result](Result.md).[isFail](Result.md#isfail)
#### Defined in
[packages/result/src/Result.ts:385](https://github.com/jamesapple/standardize/blob/c5eecb2/packages/result/src/Result.ts#L385)
___
### isSuccess
**isSuccess**(): this is Success<T\>
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`**
```ts
const result = Result.invoke(JSON.parse, '{}')
if (result.isSuccess()) {
result.get() // {}
}
#### Returns
this is Success<T\>
#### Overrides
[Result](/docs/typedoc/result/classes/Result).[isSuccess](/docs/typedoc/result/classes/Result#issuccess)
#### Defined in
[packages/result/src/Result.ts:381](https://github.com/jamesapple/standardize/blob/c5eecb2/packages/result/src/Result.ts#L381)
___
### map
▸ **map**<`X`, `Y`\>(`mapSuccess`): [`Result`](/docs/typedoc/result/classes/Result)<`X`, `Y`\>
Transform the data in a Result by applying a function to a contained
success value, leaving an error value untouched or optionally changing it
as well.
**`Example`**
```ts
Result.success('foo').map((value) => value.toUpperCase()) // Result<'FOO'>

Type parameters

NameType
XX
Yextends Error = Error

Parameters

NameType
mapSuccess(success: T) => X

Returns

Result<X, Y>

Overrides

Result.map

Defined in

packages/result/src/Result.ts:361


mapAsync

mapAsync<X, Y>(mapSuccess): 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

ts
Result.success('userId').mapAsync(getUser) // AsyncResult<User>

Type parameters

NameType
XX
Yextends Error = Error

Parameters

NameType
mapSuccess(success: T) => Promise<X>

Returns

AsyncResult<X, Y>

Overrides

Result.mapAsync

Defined in

packages/result/src/Result.ts:371


mapFailure

mapFailure<X>(mapFailure): Result<T, X>

Transform the failure value by applying a function to it.

Example

ts
Result.invoke(JSON.parse, '{')
.mapFailure((error) => new ParsingError(error.message)) // Result<unknown, string>

Type parameters

NameType
Xextends Error

Parameters

NameType
mapFailure(failed: Error) => X

Returns

Result<T, X>

Inherited from

Result.mapFailure

Defined in

packages/result/src/Result.ts:165


mapFailureAsync

mapFailureAsync<X>(mapFailure): AsyncResult<T, X>

Transform the failure value by applying a function to it asynchronously.

Example

ts
Result.invoke(JSON.parse, '{')
.mapFailureAsync(enrichErrorAsync) // AsyncResult<any, ParsingError>
#### Type parameters
| Name | Type |
| :------ | :------ |
| `X` | extends `Error` |
#### Parameters
| Name | Type |
| :------ | :------ |
| `mapFailure` | (`failed`: `Error`) => `Promise`<`X`\> |
#### Returns
[`AsyncResult`](AsyncResult.md)<`T`, `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)<`T` \| `R`, `FF`\>
Attempt to recover from a failure by mapping the error value to a success
value. If the result is a success, the mapping function is ignored.
If the result is a failure, the mapping function is invoked and the
result is returned. If the mapping function throws an error, the result
will be a failure.
**`Example`**
```ts
Result.invoke(JSON.parse, '{')
.recover((error) => ({})) // Result<{}, Error>
#### Type parameters
| Name | Type |
| :------ | :------ |
| `R` | `R` |
| `FF` | extends `Error` = `Error` |
#### Parameters
| Name | Type |
| :------ | :------ |
| `mapError` | (`err`: `Error`) => `R` |
#### Returns
[`Result`](/docs/typedoc/result/classes/Result)<`T` \| `R`, `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)<`T` \| `R`, `Error`\>
Attempt to recover from a failure by mapping the error value to a success
value. If the result is a success, the mapping function is ignored.
If the result is a failure, the mapping function is invoked and the
result is returned. If the mapping function throws an error, the result
will be a failure.
**`Example`**
```ts
await Result.invoke(JSON.parse, '{')
.recoverAsync(loadDefaultData) // AsyncResult<DefaultData, Error>

Type parameters

Name
R

Parameters

NameType
mapError(err: Error) => Promise<R>

Returns

AsyncResult<T | R, Error>

Inherited from

Result.recoverAsync

Defined in

packages/result/src/Result.ts:325


tap

tap(tapSuccess, tapError?): Result<T, Error>

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

ts
Result.invoke(JSON.parse, '{}')
.tap(console.log, console.error)
.map(...)

Parameters

NameType
tapSuccess(success: T) => unknown
tapError?(error: Error) => unknown

Returns

Result<T, Error>

Inherited from

Result.tap

Defined in

packages/result/src/Result.ts:197


tapAsync

tapAsync(tapSuccess, tapError?): AsyncResult<T, Error>

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

ts
await Result
.invoke(JSON.parse, '{}')
.tapAsync(console.log, console.error)
.map(...)

Parameters

NameType
tapSuccess(success: T) => Promise<unknown>
tapError?(error: Error) => Promise<unknown>

Returns

AsyncResult<T, Error>

Inherited from

Result.tapAsync

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

ts
Result.fail(new Error('foo')).map(...)

Type parameters

NameType
Tunknown
Fextends Error = Error

Parameters

NameType
valueF

Returns

Result<T, F>

Inherited from

Result.fail

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

ts
Result.invoke(JSON.parse, '{}').map(...)

Type parameters

NameType
FNextends (...args: any[]) => any

Parameters

NameType
throwableFN
...argsParameters<FN>

Returns

Result<ReturnType<FN>, Error>

Inherited from

Result.invoke

Defined in

packages/result/src/Result.ts:13


of

Static of<E>(value): Success<E>

Type parameters

Name
E

Parameters

NameType
valueE

Returns

Success<E>

Defined in

packages/result/src/Result.ts:357


success

Static success<T, F>(value): Result<T, F>

Create a successful result. Useful for testing

Example

ts
Result.success('foo').map(...)

Type parameters

NameType
TT
Fextends Error = Error

Parameters

NameType
valueT

Returns

Result<T, F>

Inherited from

Result.success

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

ts
const safeParse = Result.wrap(JSON.parse)
safeParse('{}').map(...)

Type parameters

NameType
FNextends (...args: any[]) => any

Parameters

NameType
fnFN

Returns

fn

▸ (...args): FN extends (...args: any[]) => RT ? Result<RT, Error> : never

Parameters
NameType
...argsParameters<FN>
Returns

FN extends (...args: any[]) => RT ? Result<RT, Error> : never

Inherited from

Result.wrap

Defined in

packages/result/src/Result.ts:35