Skip to content

Class.ResultAsync

GitHub Actions edited this page May 22, 2025 · 28 revisions

resultar / ResultAsync

Class: ResultAsync<T, E>

Defined in: result-async.ts:67

Represents an asynchronous Result type that wraps a Promise of a Result<T, E>. This class provides a way to handle asynchronous operations that may succeed with a value of type T or fail with an error of type E.

Implements

Example

// Create a successful async result
const okAsync = ResultAsync.okAsync(42);

// Create a failed async result
const errAsync = ResultAsync.errAsync(new Error("Something went wrong"));

// Transform a Promise into a ResultAsync
const resultFromPromise = ResultAsync.fromPromise(
  fetch("https://api.example.com/data"),
  (error) => new Error(`API call failed: ${error}`)
);

Remarks

ResultAsync implements the PromiseLike interface, allowing it to be used with async/await syntax. It provides various utility methods for transforming and combining results, similar to the Result type, but operating in an asynchronous context.

The class includes methods for:

  • Creating ResultAsync instances (okAsync, errAsync, fromPromise)
  • Transforming values (map, mapErr)
  • Chaining operations (andThen, orElse)
  • Error handling (tapError)
  • Conditional branching (if)
  • Combining multiple ResultAsync instances (combine, combineWithAllErrors)

Type Parameters

T

T

The type of the success value

E

E

The type of the error value

Implements

Constructors

Constructor

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

Defined in: result-async.ts:227

Parameters

res

Promise<Result<T, E>>

Returns

ResultAsync<T, E>

Methods

[asyncIterator]()

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

Defined in: result-async.ts:424

Returns

AsyncGenerator<Result<never, E>, T>


andThen()

Call Signature

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

Defined in: result-async.ts:262

Type Parameters
R

R extends Result<unknown, unknown>

Parameters
f

(t) => R

Returns

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

Call Signature

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

Defined in: result-async.ts:265

Type Parameters
R

R extends ResultAsync<unknown, unknown>

Parameters
f

(t) => R

Returns

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

Call Signature

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

Defined in: result-async.ts:268

Type Parameters
U

U

F

F

Parameters
f

(t) => Result<U, F> | ResultAsync<U, F>

Returns

ResultAsync<U, E | F>


finally()

finally(f): DisposableResultAsync<T, E>

Defined in: result-async.ts:409

Parameters

f

(value, error) => void

Returns

DisposableResultAsync<T, E>


if()

if(fCondition): object

Defined in: result-async.ts:285

Parameters

fCondition

(t) => boolean

Returns

object

true()

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

Type Parameters
X1

X1

Y1

Y1

Parameters
fTrue

(t) => ResultAsync<X1, Y1>

Returns

object

false()

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

Type Parameters
X2

X2

Y2

Y2

Parameters
fFalse

(t) => ResultAsync<X2, Y2>

Returns

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


log()

log(f): ResultAsync<T, E>

Defined in: result-async.ts:344

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

Parameters

f

(t?, e?) => void | Promise<void>

The function to call if the result is Ok

Returns

ResultAsync<T, E>

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


map()

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

Defined in: result-async.ts:248

Type Parameters

X

X

Parameters

f

(t) => X | Promise<X>

Returns

ResultAsync<X, E>


mapErr()

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

Defined in: result-async.ts:238

Type Parameters

U

U

Parameters

f

(t) => U | Promise<U>

Returns

ResultAsync<T, U>


match()

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

Defined in: result-async.ts:326

Type Parameters

A

A

B

B = A

Parameters

ok

(t) => A

fnErr

(e) => B

Returns

Promise<A | B>


orElse()

Call Signature

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

Defined in: result-async.ts:305

Type Parameters
R

R extends Result<unknown, unknown>

Parameters
f

(e) => R

Returns

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

Call Signature

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

Defined in: result-async.ts:308

Type Parameters
R

R extends ResultAsync<unknown, unknown>

Parameters
f

(e) => R

Returns

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

Call Signature

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

Defined in: result-async.ts:311

Type Parameters
U

U

A

A

Parameters
f

(e) => Result<U, A> | ResultAsync<U, A>

Returns

ResultAsync<T | U, A>


safeUnwrap()

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

Defined in: result-async.ts:446

Returns

AsyncGenerator<Result<never, E>, T>

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.


tap()

tap(f): ResultAsync<T, E>

Defined in: result-async.ts:370

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

Parameters

f

(t) => void | Promise<void>

The function to call if the result is Ok

Returns

ResultAsync<T, E>

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


tapError()

tapError(f): ResultAsync<T, E>

Defined in: result-async.ts:393

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

Parameters

f

(e) => void | Promise<void>

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


then()

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

Defined in: result-async.ts:231

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

Type Parameters

A

A

B

B

Parameters

successCallback?

(res) => A | PromiseLike<A>

failureCallback?

(reason) => B | PromiseLike<B>

Returns

PromiseLike<A | B>

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

Implementation of

PromiseLike.then


unwrapOr()

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

Defined in: result-async.ts:330

Type Parameters

A

A

Parameters

t

A

Returns

Promise<T | A>


unwrapOrThrow()

unwrapOrThrow(): Promise<T>

Defined in: result-async.ts:334

Returns

Promise<T>


combine()

Call Signature

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

Defined in: result-async.ts:172

Type Parameters
T

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

Parameters
asyncResultList

T

Returns

CombineResultAsyncs<T>

Call Signature

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

Defined in: result-async.ts:175

Type Parameters
T

T extends readonly ResultAsync<unknown, unknown>[]

Parameters
asyncResultList

T

Returns

CombineResultAsyncs<T>


combineWithAllErrors()

Call Signature

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

Defined in: result-async.ts:184

Type Parameters
T

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

Parameters
asyncResultList

T

Returns

CombineResultsWithAllErrorsArrayAsync<T>

Call Signature

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

Defined in: result-async.ts:187

Type Parameters
T

T extends readonly ResultAsync<unknown, unknown>[]

Parameters
asyncResultList

T

Returns

CombineResultsWithAllErrorsArrayAsync<T>


errAsync()

Call Signature

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

Defined in: result-async.ts:96

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

Type Parameters
T

T = never

E

E = unknown

Parameters
err

E

Returns

ResultAsync<T, E>

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

Call Signature

static errAsync<T, E>(err): ResultAsync<T, void>

Defined in: result-async.ts:98

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

Type Parameters
T

T = never

E

E extends void = void

Parameters
err

void

Returns

ResultAsync<T, void>

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


fromPromise()

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

Defined in: result-async.ts:163

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

Type Parameters

T

T

E

E

Parameters

promise

PromiseLike<T>

The promise to be wrapped in a ResultAsync.

errorFn

(e) => E

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.


fromSafePromise()

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

Defined in: result-async.ts:147

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

Type Parameters

T

T

E

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.


fromThrowable()

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

Defined in: result-async.ts:207

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

A extends readonly any[]

T

T

E

E

Parameters

fn

(...args) => Promise<T>

function to wrap with ok on success or err on failure

errorFn?

(err) => E

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

Returns

a new function that returns a ResultAsync

(...args): ResultAsync<T, E>

Parameters
args

...A

Returns

ResultAsync<T, E>


okAsync()

Call Signature

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

Defined in: result-async.ts:74

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

Type Parameters
T

T

E

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.

Call Signature

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

Defined in: result-async.ts:76

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

Type Parameters
T

T extends void = void

E

E = never

Parameters
value

void

The value to be wrapped in a Result.ok.

Returns

ResultAsync<void, E>

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


tryCatch()

static tryCatch<T, E>(fn, errorFn?): ResultAsync<T, E>

Defined in: result-async.ts:124

Creates a ResultAsync from a Promise, catching any errors that occur during its execution.

Type Parameters

T

T

E

E

Parameters

fn

The Promise or a function returning a Promise to be wrapped in a ResultAsync.

Promise<T> | () => Promise<T>

errorFn?

(e) => E

Optional function to transform the caught error into a specific error type. If not provided, the original error will be used.

Returns

ResultAsync<T, E>

A ResultAsync that will resolve to Ok with the promise's value if successful, or Err with either the transformed error (if errorFn is provided) or the original error.

Example

// Without error transformer
const result = ResultAsync.tryCatch(Promise.resolve(42));

// With error transformer
const result = ResultAsync.tryCatch(
  fetch('https://api.example.com'),
  (error) => new CustomError('API request failed')
);

unitAsync()

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

Defined in: result-async.ts:86

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

Type Parameters

E

E = never

Returns

ResultAsync<undefined, E>

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

Clone this wiki locally