githubEdit

ResultAsync

AsyncResult represents a Promise that never rejects of an operation that can either succeed (Ok) or return an error (Error). Every AsyncResult resolves to a Result.Ok when successful or Result.Error when it fails.

An AsyncResult allows you to chain the same methods as a Result, but in an asynchronous context. This empowers you to write code and manipulate data in a seamless, synchronous-like manner without worrying about awaiting Promises.

By awaiting the AsyncResult, the Promise inside will resolve to the underlying Result.

Constructors

ok

Constructs an AsyncResult that resolves to a Result.Ok with the provided value.

import { AsyncResult } from 'funkcia';

//      β”Œβ”€β”€β”€ AsyncResult<number, never>
//      β–Ό
const result = AsyncResult.ok(10);
// Promise<Ok(10)>

of

circle-info

Alias of AsyncResult.ok

Constructs an AsyncResult that resolves to a Result.Ok with the provided value.

import { AsyncResult } from 'funkcia';

//      β”Œβ”€β”€β”€ AsyncResult<number, never>
//      β–Ό
const result = AsyncResult.of(10);
// Promise<Ok(10)>

error

Constructs an AsyncResult that resolves to a Result.Error with the provided value.

fromNullable

Constructs an AsyncResult from a nullable value.

If the value is null or undefined, it resolves to a Result.Error with a NoValueError exception or a custom error. Otherwise, it resolves to a Result.Ok.

fromFalsy

Constructs an AsyncResult from a falsy value.

If the value is falsy, it resolves to a Result.Error with a NoValueError exception or a custom error. Otherwise, it resolves to a Result.Ok.

try

Constructs an AsyncResult from a promise that may reject or return a Result.

Provides multiple overloads for different promise return types and error handling strategies.

promise

Constructs an AsyncResult from a Promise that returns a Result, and never rejects.

liftPromise

Lifts a Promise that may fail or resolve to a Result to a function that returns an AsyncResult. Provides multiple overloads for different promise return types and error handling strategies.

predicate

Returns a function that asserts that a value passes the test implemented by the provided function. Provides multiple overloads for type guards and regular predicates, with optional custom error handling.

Combinators

values

Given an array of AsyncResults, returns an array containing only the values inside Result.Ok.

zip

Combines two AsyncResults into a single AsyncResult containing a tuple of their values, if both AsyncResults resolve to Result.Ok variants, otherwise, resolves to Result.Error.

zipWith

Combines two AsyncResults into a single AsyncResult. The new value is produced by applying the given function to both values, if both AsyncResults resolve to Result.Ok variants, otherwise, resolves to Result.Error.

Conversions

then

Attaches a callback for the resolution of the Promise inside the AsyncResult.

match

Returns a promise that compares the underlying Result against the possible patterns, and then execute code based on which pattern matches.

unwrap

Returns a promise that unwraps the underlying AsyncResult value. Throws UnwrapError if the Result is Error.

unwrapError

Returns a promise that unwraps the underlying Result error. Throws UnwrapError if the Result is Ok.

unwrapOr

Returns a promise that unwraps the underlying Result. If the promise resolves to a Result.Error, returns the result of the provided callback.

unwrapOrNull

Returns a promise that unwraps the value of the underlying Result if it is a Result.Ok, otherwise returns null.

unwrapOrUndefined

Returns a promise that unwraps the value of the underlying Result if it is a Result.Ok, otherwise returns undefined.

expect

Returns a promise that unwraps the underlying Result value. Throws the provided Error if the Result is Error.

merge

Returns a promise that unwraps the underlying Result. If the Result is Ok, resolves to the value inside the Ok variant. If the Result is Error, resolves to the value inside the Error variant.

contains

Returns a Promise that verifies if the Result contains a value that passes the test implemented by the provided function.

toAsyncOption

Converts the AsyncResult to an AsyncOption. If the resolved Result is Ok, returns an AsyncOption.Some. If the resolved Result is Error, returns an AsyncOption.None.

toArray

Returns a Promise that converts the underlying Result to an array. If the resolved Result is Ok, returns an array with the value. If the resolved Result is Error, returns an empty array.

Transformations

map

Applies a callback function to the value of the AsyncResult when it is Ok, returning a new AsyncResult containing the new value.

mapError

Applies a callback function to the value of the AsyncResult when it is Error, returning a new AsyncResult containing the new error value.

andThen

Applies a callback function to the value of the AsyncResult when it is Ok, and returns the new value. Supports both Result and AsyncResult returns.

filter

Asserts that the AsyncResult value passes the test implemented by the provided function. Supports type guards and regular predicates with optional custom error handling.

Fallbacks

or

Replaces the current AsyncResult with the provided fallback AsyncResult when it is Error. If the resolved Result is Ok, it returns the current AsyncResult.

swap

Swaps the AsyncResult value and error.

If the underlying Result is Ok, it returns a AsyncResult that resolves to a Result.Error with the value. If the underlying Result is Error, it returns a AsyncResult that resolves to a Result.Ok with the error.

Other Utilities

tap

Calls the function with Result value, then returns the Result itself. The return value of the provided function is ignored.

tapError

Calls the function with the underlying Result error, then returns the AsyncResult itself. The return value of the provided function is ignored.

Last updated

Was this helpful?