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
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.
import { AsyncResult } from 'funkcia';
async function rateLimit(clientId: ClientId, ip: IpAddress): AsyncResult<ClientId, RateLimitError> {
const attempts = await cache.get(`ratelimit:${clientId}:${ip}`)
if (attempts.total > 10) {
return AsyncResult.error(new RateLimitError({ clientId, ip }));
}
return AsyncOption.ok(clientId);
}
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.
import { AsyncResult } from 'funkcia';
// With default error handling
// ┌─── AsyncResult<string, NoValueError>
// â–¼
const result = AsyncResult.fromNullable(localStorage.getItem('@app/theme'));
// With custom error handling
// ┌─── AsyncResult<string, Error>
// â–¼
const result = AsyncResult.fromNullable(
localStorage.getItem('@app/theme'),
() => new Error('Theme not set'),
);
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.
import { AsyncResult } from 'funkcia';
interface User {
id: string;
firstName: string;
lastName: string | null;
}
// With default error handling
// ┌─── AsyncResult<string, NoValueError>
// â–¼
const result = AsyncResult.fromFalsy(user.lastName?.trim());
// With custom error handling
// ┌─── AsyncResult<string, Error>
// â–¼
const result = AsyncResult.fromFalsy(
user.lastName?.trim(),
() => new Error('User missing last name'),
);
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.
import { AsyncResult } from 'funkcia';
// With Result-returning promise
declare async function findUserById(id: string): Promise<Result<User, UserNotFound>>;
// ┌─── AsyncResult<User, UserNotFound | UnknownError>
// â–¼
const url = AsyncResult.try(() => findUserById('user_123'));
// Output: Promise<Ok(User)>
// With value-returning promise
declare async function findUserByIdOrThrow(id: string): Promise<User>;
// ┌─── AsyncResult<User, UnknownError>
// â–¼
const url = AsyncResult.try(() => findUserByIdOrThrow('user_123'));
// Output: Promise<Error(UnknownError)>
// With custom error handling
// ┌─── AsyncResult<User, UserNotFound | DatabaseFailureError>
// â–¼
const url = AsyncResult.try(
() => findUserByIdOrThrow('user_123'),
(error) => UserNotFound.is(error) ? error : new DatabaseFailureError(error),
);
// Output: Promise<Error(DatabaseFailureError('Error: Failed to connect to the database'))>
promise
Constructs an AsyncResult from a Promise that returns a Result, and never rejects.
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.
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.
import { AsyncResult } from 'funkcia';
// With type guard
declare const input: Shape;
// ┌─── (shape: Shape) => AsyncResult<Circle, FailedPredicateError<Square>>
// â–¼
const ensureCircle = AsyncResult.predicate(
(shape: Shape): shape is Circle => shape.kind === 'circle',
);
// ┌─── AsyncResult<Circle, FailedPredicateError<Square>>
// â–¼
const result = ensureCircle(input);
// With regular predicate
// ┌─── (value: number) => AsyncResult<number, FailedPredicateError<number>>
// â–¼
const ensurePositive = AsyncResult.predicate(
(value: number) => value > 0,
);
// ┌─── AsyncResult<number, FailedPredicateError<number>>
// â–¼
const result = ensurePositive(10);
// Output: Ok(10)
// With custom error handling
// ┌─── (shape: Shape) => AsyncResult<Circle, InvalidShapeError>
// â–¼
const ensureCircle = AsyncResult.predicate(
(shape: Shape): shape is Circle => shape.kind === 'circle',
// ┌─── Square
// â–¼
(shape) => new InvalidShapeError(shape.kind),
);
Combinators
values
Given an array of AsyncResults, returns an array containing only the values inside Result.Ok.
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.
import { AsyncResult } from 'funkcia';
const first = AsyncResult.some('hello');
const second = AsyncResult.some('world');
// ┌─── AsyncResult<[string, string], never>
// â–¼
const strings = first.zip(second);
// Output: Promise<Ok(['hello', 'world'])>
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.
import { AsyncResult } from 'funkcia';
const first = AsyncResult.some('hello');
const second = AsyncResult.some('world');
// ┌─── AsyncResult<string, never>
// â–¼
const greeting = first.zipWith(second, (a, b) => `${a} ${b}`);
// Output: Promise<Ok('hello world')>
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.
import { AsyncResult } from 'funkcia';
import { readFileSync } from 'node:fs';
declare function readFile(path: string): AsyncResult<string, FileNotFoundError | FileReadError>;
declare function parseJsonFile(contents: string): AsyncResult<FileContent, InvalidJsonError>;
// ┌─── string
// â–¼
const data = await readFile('data.json')
.andThen(parseJsonFile)
.match({
Ok(contents) {
return 'File is valid JSON';
},
// ┌─── FileNotFoundError | FileReadError | InvalidJsonError
// â–¼
Error(error) {
return 'File is invalid JSON';
},
});
unwrap
Returns a promise that unwraps the underlying AsyncResult value. Throws UnwrapError if the Result is Error.
import { AsyncResult } from 'funkcia';
// ┌─── User
// â–¼
const user = await AsyncResult.ok(databaseUser).unwrap();
const team = await AsyncResult.error(new TeamNotFound()).unwrap();
// Output: Uncaught exception: 'called "Result.unwrap()" on an "Error" value'
unwrapError
Returns a promise that unwraps the underlying Result error. Throws UnwrapError if the Result is Ok.
Returns a promise that unwraps the value of the underlying Result if it is a Result.Ok, otherwise returns null.
import { AsyncResult } from 'funkcia';
// ┌─── User | null
// â–¼
const user = await AsyncResult.ok(databaseUser).unwrapOrNull();
unwrapOrUndefined
Returns a promise that unwraps the value of the underlying Result if it is a Result.Ok, otherwise returns undefined.
import { AsyncResult } from 'funkcia';
// ┌─── User | undefined
// â–¼
const user = await AsyncResult.ok(databaseUser).unwrapOrUndefined();
expect
Returns a promise that unwraps the underlying Result value. Throws the provided Error if the Result is Error.
import { AsyncResult } from 'funkcia';
declare function findUserById(id: string): AsyncResult<User, DatabaseFailureError>;
// ┌─── User
// â–¼
const user = await findUserById('user_123').expect(
(error) => new UserNotFound(userId),
// â–²
// └─── DatabaseFailureError
);
const anotherUser = await findUserById('invalid_id').expect(
(error) => new UserNotFound('team_123'),
// â–²
// └─── DatabaseFailureError
);
// Output: Uncaught exception: 'User not found: "user_123"'
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.
import { AsyncResult } from 'funkcia';
declare function getCachedUser(email: Email): AsyncResult<User, CacheMissError<Email>>;
declare function findOrCreateUserByEmail(email: Email): AsyncResult<User, never>;
// ┌─── User
// â–¼
const result = await getCachedUser('johndoe@example.com')
.swap() // AsyncResult<CacheMissError<Email>, User>
.andThen((cacheMiss) => findOrCreateUserByEmail(cacheMiss.input)) // AsyncResult<User, User>
.merge();
// Output: { id: 'user_123', email: 'johndoe@example.com' }
contains
Returns a Promise that verifies if the Result contains a value that passes the test implemented by the provided function.
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.
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.
Applies a callback function to the value of the AsyncResult when it is Ok, returning a new AsyncResult containing the new value.
import { AsyncResult } from 'funkcia';
// ┌─── AsyncResult<number, never>
// â–¼
const result = AsyncResult.ok(10).map(number => number * 2);
// Output: Promise<Ok(20)>
mapError
Applies a callback function to the value of the AsyncResult when it is Error, returning a new AsyncResult containing the new error value.
import { AsyncResult } from 'funkcia';
// ┌─── AsyncResult<string, UserMissingInformationError>
// â–¼
const result = AsyncResult.fromNullable(user.lastName).mapError(
(error) => new UserMissingInformationError()
// â–²
// └─── NoValueError
);
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.
import { AsyncResult } from 'funkcia';
// With Result return
declare function readFile(path: string): AsyncResult<string, FileNotFoundError | FileReadError>;
declare function parseJsonFile(contents: string): Result<FileContent, InvalidJsonError>;
// ┌─── AsyncResult<FileContent, FileNotFoundError | FileReadError | InvalidJsonError>
// â–¼
const result = readFile('data.json')
.andThen(parseJsonFile);
// With AsyncResult return
declare function parseJsonFileAsync(contents: string): AsyncResult<FileContent, InvalidJsonError>;
// ┌─── AsyncResult<FileContent, FileNotFoundError | FileReadError | InvalidJsonError>
// â–¼
const result = readFile('data.json')
.andThen(parseJsonFileAsync);
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.
import { AsyncResult } from 'funkcia';
// With type guard
declare const input: Shape;
// ┌─── AsyncResult<Circle, FailedPredicateError<Square>>
// â–¼
const result = AsyncResult.of(input).filter(
(shape): shape is Circle => shape.kind === 'CIRCLE',
);
// With regular predicate
// ┌─── AsyncResult<string, FailedPredicateError<string>>
// â–¼
const result = AsyncResult.of(user.lastName).filter(
(value) => value.length > 0,
);
// With custom error handling
// ┌─── AsyncResult<string, Error>
// â–¼
const result = AsyncResult.of(user.lastName).filter(
(value) => value.length > 0,
(value) => new Error(`Expected non-empty string, received ${value}`),
);
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.
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.
import { AsyncResult } from 'funkcia';
declare function getCachedUser(email: Email): AsyncResult<User, CacheMissError<Email>>;
declare function findOrCreateUserByEmail(email: Email): AsyncResult<User, never>;
// ┌─── Result<User, User>
// â–¼
const result = getCachedUser('johndoe@example.com')
.swap()
.andThen((cacheMiss) => findOrCreateUserByEmail(cacheMiss.input));
// â–²
// └─── CacheMissError<Email>
Other Utilities
tap
Calls the function with Result value, then returns the Result itself. The return value of the provided function is ignored.
import { AsyncResult } from 'funkcia';
declare function authenticateUser(credentials: AuthCredentials): AsyncResult<User, UserNotFound | InvalidCredentials>;
declare async function registerSuccessfulLoginAttempt(user: User): Promise<{ loginAttempts: number }>;
// ┌─── AsyncResult<User, UserNotFound | InvalidCredentials>
// â–¼
const result = authenticateUser(req.body).tap(async (user) => {
return await registerSuccessfulLoginAttempt(user);
});
// Output: Promise<Ok(User)>
tapError
Calls the function with the underlying Result error, then returns the AsyncResult itself. The return value of the provided function is ignored.
import { AsyncResult } from 'funkcia';
declare function authenticateUser(credentials: AuthCredentials): AsyncResult<User, UserNotFound | InvalidCredentials>;
declare async function registerLoginAttempt(user: User): Promise<{ loginAttempts: number }>;
// ┌─── AsyncResult<User, UserNotFound | InvalidCredentials>
// â–¼
const result = authenticateUser(req.body).tapError(async (error) => {
if (InvalidCredentials.is(error)) {
return await registerLoginAttempt(error.email);
}
});
// Output: Promise<Error(InvalidCredentials)>