AsyncOption represents a Promise that never rejects of an asynchronous optional value.
Every AsyncOption resolves to either Option.Some, containing a value, or Option.None, which is empty. It allows you to chain the same methods as an Option, but in an asynchronous context.
Constructors
some
Constructs an AsyncOption that resolves to an Option.Some containing a value.
Lifts a Promise that resolves to an Option or nullable value to a function that returns an AsyncOption.
import { AsyncOption } from 'funkcia';
// With Option return type
declare async function findUserById(id: string): Promise<Option<User>>
// ┌─── (id: string) => AsyncOption<User>
// â–¼
const safeFindUserById = AsyncOption.liftPromise(findUserById);
// ┌─── AsyncOption<User>
// â–¼
const user = safeFindUserById('user_123');
// With nullable return type
declare async function findUserById(id: string): Promise<User | null>
// ┌─── (id: string) => AsyncOption<User>
// â–¼
const safeFindUserById = AsyncOption.liftPromise(findUserById);
// ┌─── AsyncOption<User>
// â–¼
const user = safeFindUserById('user_123');
predicate
Returns a function that asserts that a value passes the test implemented by the provided function. Can create an AsyncOption that resolves to either Some with a narrowed type or None.
Combines two AsyncOptions into a single AsyncOption containing a tuple of their values, if both AsyncOptions are Some variants, otherwise, returns None.
import { AsyncOption } from 'funkcia';
const first = AsyncOption.some('hello');
const second = AsyncOption.some('world');
// ┌─── AsyncOption<[string, string]>
// â–¼
const strings = first.zip(second);
// Output: Promise<Some(['hello', 'world'])>
zipWith
Combines two AsyncOptions into a single AsyncOption. The new value is produced by applying the given function to both values, if both AsyncOptions are Some variants, otherwise, returns None.
import { AsyncOption } from 'funkcia';
const first = AsyncOption.some('hello');
const second = AsyncOption.some('world');
// ┌─── AsyncOption<string>
// â–¼
const greeting = first.zipWith(second, (a, b) => `${a} ${b}`);
// Output: Promise<Some('hello world')>
Conversions
then
Attaches a callback for the resolution of the Promise inside the AsyncOption.
Applies a callback function to the value of the AsyncOption when it is Some, and returns the new value. Can work with both Option and AsyncOption returns.
import { AsyncOption } from 'funkcia';
// With Option return
declare function readFile(path: string): AsyncOption<string>;
declare function parseJsonFile(contents: string): Option<FileContent>;
// ┌─── AsyncOption<FileContent>
// â–¼
const option = readFile('data.json').andThen(parseJsonFile);
// With AsyncOption return
declare function parseJsonFileAsync(contents: string): AsyncOption<FileContent>;
// ┌─── AsyncOption<FileContent>
// â–¼
const option = readFile('data.json').andThen(parseJsonFileAsync);
filter
Asserts that the AsyncOption value passes the test implemented by the provided function. Can narrow types.
import { AsyncOption } from 'funkcia';
// With type guard
declare const input: Shape;
// ┌─── AsyncOption<Circle>
// â–¼
const circle = AsyncOption.of(input).filter(
(shape): shape is Circle => shape.kind === 'circle',
);
// With regular predicate
// ┌─── AsyncOption<User>
// â–¼
const option = AsyncOption.of(user).filter((user) => user.age >= 21);
Fallbacks
or
Replaces the current AsyncOption with the provided fallback AsyncOption when it is None.