ResultAsync
ResultAsync represents a Promise that never rejects of an operation that can either succeed (Ok) or return an error (Error). Every ResultAsync resolves to a Result.Ok when successful or Result.Error when it fails.
An ResultAsync 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 ResultAsync, the Promise inside will resolve to the underlying Result.
Defects vs Domain Errors
Domain errors are expected failures represented with
Result.Error.Defects are unexpected throw/reject behavior in callback code and are surfaced as
Panic.Best practice: expected failures should be returned as
Error, not thrown.
Domain validation or business failure
Resolve to Result.Error(...)
ResultAsync.try(() => promise) promise rejects
Resolves to Result.Error(UnhandledException) or your mapped custom error
ResultAsync.let(...) callback throws/rejects
Treated as a defect and throws Panic
ResultAsync.tap(...) / ResultAsync.tapError(...) callback throws/rejects
Treated as a defect and throws Panic
Static Methods
ok
Constructs an ResultAsync that resolves to a Result.Ok with the provided value.
import { ResultAsync } from 'funkcia';
// ββββ ResultAsync<void, never>
// βΌ
const empty = ResultAsync.ok();
// ββββ ResultAsync<number, never>
// βΌ
const result = ResultAsync.ok(10);of
Alias of ResultAsync.ok
Constructs an ResultAsync that resolves to a Result.Ok with the provided value.
error
Constructs an ResultAsync that resolves to a Result.Error with the provided value.
fromNullable
Constructs an ResultAsync 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 ResultAsync 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.
fromResult
Converts a synchronous Result into a ResultAsync.
fromOption
Converts an Option into a ResultAsync.
If the Option is Some, it resolves to Result.Ok. If it is None, it resolves to Result.Error(NoValueError) or your custom error.
fromOptionAsync
Converts an OptionAsync into a ResultAsync.
If the OptionAsync resolves to Some, it resolves to Result.Ok. If it resolves to None, it resolves to Result.Error(NoValueError) or your custom error.
try
Constructs a ResultAsync from a promise that may reject.
If the promise resolves, the value is wrapped in Result.Ok. If it rejects, the error is wrapped in Result.Error using UnhandledException or your custom mapper.
fn
Declares a function that returns either:
a
Promise<Result<...>>, oran async generator for propagation flows.
It returns a new function that produces ResultAsync.
resource
Wraps a resource and provides a safe way to execute operations with error handling.
With custom error mapping:
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 ResultAsyncs, returns an array containing only the values inside Result.Ok.
zip
Combines two ResultAsyncs into a single ResultAsync containing a tuple of their values, if both ResultAsyncs resolve to Result.Ok variants, otherwise, resolves to Result.Error.
zipWith
Combines two ResultAsyncs into a single ResultAsync. The new value is produced by applying the given function to both values, if both ResultAsyncs resolve to Result.Ok variants, otherwise, resolves to Result.Error.
Conversions
then
Attaches a callback for the resolution of the Promise inside the ResultAsync.
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 ResultAsync value. Throws Panic if the Result is Error.
unwrapError
Returns a promise that unwraps the underlying Result error. Throws Panic 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.
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 ResultAsync when it is Ok, returning a new ResultAsync containing the new value.
mapError
Applies a callback function to the value of the ResultAsync when it is Error, returning a new ResultAsync containing the new error value.
mapBoth
Transforms both variants of the ResultAsync in one call:
Okvalues are mapped bycases.OkErrorvalues are mapped bycases.Error
andThen
Applies a callback function to the value of the ResultAsync when it is Ok, and returns the new value. Supports both Result and ResultAsync returns.
filter
Asserts that the ResultAsync 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 ResultAsync with the provided fallback ResultAsync when it is Error. If the resolved Result is Ok, it returns the current ResultAsync.
swap
Swaps the ResultAsync value and error.
If the underlying Result is Ok, it returns a ResultAsync that resolves to a Result.Error with the value. If the underlying Result is Error, it returns a ResultAsync 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 ResultAsync itself. The return value of the provided function is ignored.
Last updated
Was this helpful?