Skip to content

Class.ResultAsync

GitHub Actions edited this page Oct 24, 2024 · 23 revisions

resultar / ResultAsync

Class: ResultAsync<T, E>

The ResultAsync type. This is a union of Result and PromiseLike<Result>

Type Parameters

T

E

Implements

Constructors

new ResultAsync()

new ResultAsync<T, E>(res): ResultAsync<T, E>

Parameters

res: Promise<Result<T, E>>

Returns

ResultAsync<T, E>

Defined in

result-async.ts:150

Methods

[asyncIterator]()

[asyncIterator](): AsyncGenerator<Result<never, E>, T, any>

Returns

AsyncGenerator<Result<never, E>, T, any>

Defined in

result-async.ts:347


andThen()

andThen(f)

andThen<R>(f): ResultAsync<InferOkTypes<R>, E | InferErrTypes<R>>

Type Parameters

R extends Result<unknown, unknown>

Parameters

f

Returns

ResultAsync<InferOkTypes<R>, E | InferErrTypes<R>>

Defined in

result-async.ts:185

andThen(f)

andThen<R>(f): ResultAsync<InferAsyncOkTypes<R>, E | InferAsyncErrTypes<R>>

Type Parameters

R extends ResultAsync<unknown, unknown>

Parameters

f

Returns

ResultAsync<InferAsyncOkTypes<R>, E | InferAsyncErrTypes<R>>

Defined in

result-async.ts:188

andThen(f)

andThen<U, F>(f): ResultAsync<U, E | F>

Type Parameters

U

F

Parameters

f

Returns

ResultAsync<U, E | F>

Defined in

result-async.ts:191


finally()

finally(f): DisposableResultAsync<T, E>

Parameters

f

Returns

DisposableResultAsync<T, E>

Defined in

result-async.ts:332


if()

if(fCondition): object

Parameters

fCondition

Returns

object

true()

true: <X1, Y1>(fTrue) => object

Type Parameters

X1

Y1

Parameters

fTrue

Returns

object

false()

false: <X2, Y2>(fFalse) => ResultAsync<X1 | X2, E | Y1 | Y2>

Type Parameters

X2

Y2

Parameters

fFalse

Returns

ResultAsync<X1 | X2, E | Y1 | Y2>

Defined in

result-async.ts:208


log()

log(f): ResultAsync<T, E>

Performs a side effect for the Ok variant of ResultAsync. This function can be used for control flow based on result values.

Parameters

f

The function to call if the result is Ok

Returns

ResultAsync<T, E>

self if the result is Err, otherwise the result of f

Defined in

result-async.ts:267


map()

map<X>(f): ResultAsync<X, E>

Type Parameters

X

Parameters

f

Returns

ResultAsync<X, E>

Defined in

result-async.ts:171


mapErr()

mapErr<U>(f): ResultAsync<T, U>

Type Parameters

U

Parameters

f

Returns

ResultAsync<T, U>

Defined in

result-async.ts:161


match()

match<A, B>(ok, fnErr): Promise<A | B>

Type Parameters

A

B = A

Parameters

ok

fnErr

Returns

Promise<A | B>

Defined in

result-async.ts:249


orElse()

orElse(f)

orElse<R>(f): ResultAsync<T | InferOkTypes<R>, InferErrTypes<R>>

Type Parameters

R extends Result<unknown, unknown>

Parameters

f

Returns

ResultAsync<T | InferOkTypes<R>, InferErrTypes<R>>

Defined in

result-async.ts:228

orElse(f)

orElse<R>(f): ResultAsync<T | InferAsyncOkTypes<R>, InferAsyncErrTypes<R>>

Type Parameters

R extends ResultAsync<unknown, unknown>

Parameters

f

Returns

ResultAsync<T | InferAsyncOkTypes<R>, InferAsyncErrTypes<R>>

Defined in

result-async.ts:231

orElse(f)

orElse<U, A>(f): ResultAsync<T | U, A>

Type Parameters

U

A

Parameters

f

Returns

ResultAsync<T | U, A>

Defined in

result-async.ts:234


safeUnwrap()

safeUnwrap(): AsyncGenerator<Result<never, E>, T, any>

Returns

AsyncGenerator<Result<never, E>, T, any>

Deprecated

will be removed in 2.0.0.

You can use safeTry without this method.

Example

safeTry(async function* () {
  const okValue = yield* yourResult
})

Emulates Rust's ? operator in safeTry's body. See also safeTry.

Defined in

result-async.ts:369


tap()

tap(f): ResultAsync<T, E>

Performs a side effect for the Ok variant of ResultAsync. This function can be used for control flow based on result values.

Parameters

f

The function to call if the result is Ok

Returns

ResultAsync<T, E>

self if the result is Err, otherwise the result of f

Defined in

result-async.ts:293


tapError()

tapError(f): ResultAsync<T, E>

Performs a side effect for the Err variant of ResultAsync. This function can be used for control flow based on result values.

Parameters

f

The function to call if the result is Err

Returns

ResultAsync<T, E>

self if the result is Ok, otherwise the result of f

Example

Defined in

result-async.ts:316


then()

then<A, B>(successCallback?, failureCallback?): PromiseLike<A | B>

Attaches callbacks for the resolution and/or rejection of the Promise.

Type Parameters

A

B

Parameters

successCallback?

failureCallback?

Returns

PromiseLike<A | B>

A Promise for the completion of which ever callback is executed.

Implementation of

PromiseLike.then

Defined in

result-async.ts:154


unwrapOr()

unwrapOr<A>(t): Promise<T | A>

Type Parameters

A

Parameters

t: A

Returns

Promise<T | A>

Defined in

result-async.ts:253


unwrapOrThrow()

unwrapOrThrow(): Promise<T>

Returns

Promise<T>

Defined in

result-async.ts:257


combine()

combine(asyncResultList)

static combine<T>(asyncResultList): CombineResultAsyncs<T>

Type Parameters

T extends readonly [ResultAsync<unknown, unknown>, ResultAsync<unknown, unknown>]

Parameters

asyncResultList: T

Returns

CombineResultAsyncs<T>

Defined in

result-async.ts:95

combine(asyncResultList)

static combine<T>(asyncResultList): CombineResultAsyncs<T>

Type Parameters

T extends readonly ResultAsync<unknown, unknown>[]

Parameters

asyncResultList: T

Returns

CombineResultAsyncs<T>

Defined in

result-async.ts:98


combineWithAllErrors()

combineWithAllErrors(asyncResultList)

static combineWithAllErrors<T>(asyncResultList): CombineResultsWithAllErrorsArrayAsync<T>

Type Parameters

T extends readonly [ResultAsync<unknown, unknown>, ResultAsync<unknown, unknown>]

Parameters

asyncResultList: T

Returns

CombineResultsWithAllErrorsArrayAsync<T>

Defined in

result-async.ts:107

combineWithAllErrors(asyncResultList)

static combineWithAllErrors<T>(asyncResultList): CombineResultsWithAllErrorsArrayAsync<T>

Type Parameters

T extends readonly ResultAsync<unknown, unknown>[]

Parameters

asyncResultList: T

Returns

CombineResultsWithAllErrorsArrayAsync<T>

Defined in

result-async.ts:110


errAsync()

static errAsync<T, E>(error): ResultAsync<T, E>

Returns a ResultAsync instance that is immediately resolved with a Result.err(error).

Type Parameters

T = never

E = unknown

Parameters

error: E

The error to be wrapped in a Result.err.

Returns

ResultAsync<T, E>

A ResultAsync instance with the given error and value type T.

Defined in

result-async.ts:59


fromPromise()

static fromPromise<T, E>(promise, errorFn): ResultAsync<T, E>

Returns a ResultAsync instance that is resolved with a Result.ok(value) or Result.err(error) based on the provided promise.

Type Parameters

T

E

Parameters

promise: PromiseLike<T>

The promise to be wrapped in a ResultAsync.

errorFn

A function that transforms the error from the promise into the error type E.

Returns

ResultAsync<T, E>

A ResultAsync instance with the given promise and error type E.

Defined in

result-async.ts:86


fromSafePromise()

static fromSafePromise<T, E>(promise): ResultAsync<T, E>

Returns a ResultAsync instance that is resolved with a Result.ok(value) or Result.err(error) based on the provided promise.

Type Parameters

T

E = never

Parameters

promise: PromiseLike<T>

The promise to be wrapped in a ResultAsync.

Returns

ResultAsync<T, E>

A ResultAsync instance with the given promise and error type E.

Defined in

result-async.ts:70


fromThrowable()

static fromThrowable<A, T, E>(fn, errorFn?): (...args) => ResultAsync<T, E>

Wraps a async function with a try catch, creating a new function with the same arguments but returning Ok if successful, Err if the function throws

Type Parameters

A extends readonly any[]

T

E

Parameters

fn

function to wrap with ok on success or err on failure

errorFn?

when an error is thrown, this will wrap the error result if provided

Returns

Function

a new function that returns a ResultAsync

Parameters

• ...args: A

Returns

ResultAsync<T, E>

Defined in

result-async.ts:130


okAsync()

static okAsync<T, E>(value): ResultAsync<T, E>

Returns a ResultAsync instance that is immediately resolved with a Result.ok(value).

Type Parameters

T

E = never

Parameters

value: T

The value to be wrapped in a Result.ok.

Returns

ResultAsync<T, E>

A ResultAsync instance with the given value and error type E.

Defined in

result-async.ts:40


unitAsync()

static unitAsync<E>(): ResultAsync<undefined, E>

Returns a ResultAsync that is immediately resolved with a Result.ok(undefined) value.

Type Parameters

E = never

Returns

ResultAsync<undefined, E>

A ResultAsync instance with undefined as the value type and E as the error type.

Defined in

result-async.ts:49

Clone this wiki locally