Skip to main content
Version: 1.x

Class: AsyncResult<T, F>

Type parameters

NameType
TT
Fextends Error = Error

Implements

Methods

flatMap

flatMap<X, XF>(mapSuccess, mapError?): UnwrappedAsyncResult<X, F | XF>

Type parameters

NameType
XX
XFextends Error = F

Parameters

NameType
mapSuccess(success: T) => Result<X, Error>
mapError?(error: F) => Result<XF, Error>

Returns

UnwrappedAsyncResult<X, F | XF>

Defined in

packages/result/src/AsyncResult.ts:237


flatMapAsync

flatMapAsync<X, XF>(mapSuccess, mapError?): UnwrappedAsyncResult<X, F | XF>

Type parameters

NameType
XX
XFextends Error = F

Parameters

NameType
mapSuccess(success: T) => AsyncResult<X, Error>
mapError?(error: F) => AsyncResult<XF, Error>

Returns

UnwrappedAsyncResult<X, F | XF>

Defined in

packages/result/src/AsyncResult.ts:246


get

get(): Promise<T>

Returns

Promise<T>

Defined in

packages/result/src/AsyncResult.ts:193


getEither

getEither(): Promise<T | F>

Returns

Promise<T | F>

Defined in

packages/result/src/AsyncResult.ts:197


map

map<X, Y>(mapSuccess, mapError?): AsyncResult<X, Y>

Type parameters

NameType
XX
Yextends Error = F

Parameters

NameType
mapSuccess(success: T) => X
mapError?(error: F) => Y

Returns

AsyncResult<X, Y>

Defined in

packages/result/src/AsyncResult.ts:219


mapAsync

mapAsync<X, Y>(mapSuccess, mapError?): AsyncResult<X, Y>

Type parameters

NameType
XX
Yextends Error = F

Parameters

NameType
mapSuccess(success: T) => Promise<X>
mapError?(error: F) => Promise<Y>

Returns

AsyncResult<X, Y>

Defined in

packages/result/src/AsyncResult.ts:228


mapFailure

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

Type parameters

NameType
Xextends Error = F

Parameters

NameType
mapFailure(failed: F) => X

Returns

AsyncResult<T, X>

Defined in

packages/result/src/AsyncResult.ts:255


tap

tap(mapSuccess, mapError?): AsyncResult<T, F>

Parameters

NameType
mapSuccess(success: T) => unknown
mapError?(error: F) => unknown

Returns

AsyncResult<T, F>

Defined in

packages/result/src/AsyncResult.ts:201


tapAsync

tapAsync(mapSuccess, mapError?): AsyncResult<T, F>

Parameters

NameType
mapSuccess(success: T) => Promise<unknown>
mapError?(error: F) => Promise<unknown>

Returns

AsyncResult<T, F>

Defined in

packages/result/src/AsyncResult.ts:210


then

then<TResult1, TResult2>(onfulfilled?, _onrejected?): Promise<TResult1 | TResult2>

Adheres to the interface PromiseLike<Result<T, F>> which allows using await and .then() to allow total interop with promises.

Type parameters

NameType
TResult1Result<T, F>
TResult2never

Parameters

NameType
onfulfilled?null | (value: Result<T, F>) => TResult1 | PromiseLike<TResult1>
_onrejected?null | (reason: any) => TResult2 | PromiseLike<TResult2>

Returns

Promise<TResult1 | TResult2>

Implementation of

PromiseLike.then

Defined in

packages/result/src/AsyncResult.ts:57


withTimeout

withTimeout<E>(ms, error?): AsyncResult<T, F | E>

Returns a new AsyncResult that will fail if the original AsyncResult does not resolve within the given timeout.

Example

ts
const result = await AsyncResult
.success(userId)
.withTimeout(1_000)
.flatMap(doSomethingWithUserId) // AsyncResult<Data, ResultTimeout>

Type parameters

NameType
Eextends Error = ResultTimeoutError

Parameters

NameTypeDescription
msnumberThe timeout in milliseconds
error?EThe error to throw if the timeout is exceeded. Defaults to ResultTimeoutError

Returns

AsyncResult<T, F | E>

Defined in

packages/result/src/AsyncResult.ts:164


fail

Static fail<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>

Defined in

packages/result/src/AsyncResult.ts:81


fromPromise

Static fromPromise<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>

Defined in

packages/result/src/AsyncResult.ts:108


invoke

Static invoke<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>

Defined in

packages/result/src/AsyncResult.ts:46


success

Static success<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>

Defined in

packages/result/src/AsyncResult.ts:95


unwrapResult

Static unwrapResult<R>(promiseOrResult): Awaited<R> extends Result<T, F> ? UnwrappedAsyncResult<T, F> : never

Unwraps results that have somehow been wrapped in promises or are wrapping promises internally promises internally

Example

Types that should resolve to AsyncResult<T, Error>:

typescript
type Unwrappable =
| Promise<Result<Promise<T>, Error>>
| Promise<Result<T, Promise<Error>>>
| Promise<Result<Promise<T>, Promise<Error>>>
| AsyncResult<T, Error>
| Result<T, Error>

Type parameters

Name
R

Parameters

NameType
promiseOrResultR

Returns

Awaited<R> extends Result<T, F> ? UnwrappedAsyncResult<T, F> : never

Defined in

packages/result/src/AsyncResult.ts:133


wrap

Static wrap<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

Defined in

packages/result/src/AsyncResult.ts:24