githubEdit

OptionAsync

OptionAsync represents a Promise that never rejects of an asynchronous optional value.

Every OptionAsync 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.

Defects vs Domain Errors

  • Domain absence is expected and represented with Option.None.

  • Defects are unexpected throw/reject behavior in callback code and are surfaced as Panic.

  • Best practice: expected failures should be returned as None, not thrown.

Scenario
Behavior

Missing/invalid value in normal async control flow

Resolve to Option.None

OptionAsync.try(() => promise) promise rejects or resolves to nullable

Resolves to Option.None

OptionAsync.let(...) callback throws/rejects or returns nullable

Resolves to Option.None

OptionAsync.tap(...) callback throws/rejects

Treated as a defect and throws Panic

Static Methods

some

Constructs an OptionAsync that resolves to an Option.Some containing a value.

import { OptionAsync } from 'funkcia';

//         β”Œβ”€β”€β”€ OptionAsync<number>
//         β–Ό
const asyncOption = OptionAsync.some(10);

const option = await asyncOption;
//       β–²
//       └─── Option<number>

of

circle-info

Alias of OptionAsync.some

Constructs an OptionAsync that resolves to a Some Option containing a value.

none

Constructs an OptionAsync that resolves to a None Option.

fromNullable

Constructs an OptionAsync from a nullable value.

If the value is null or undefined, resolves to an Option.None. Otherwise, resolves to an Option.Some with the value.

fromFalsy

Constructs an OptionAsync from a falsy value.

If the value is falsy, resolves to a None. Otherwise, resolves to a Some with the value.

try

Constructs an OptionAsync from a Promise that may reject.

If the promise rejects, or resolves to null or undefined, resolves to an Option.None. Otherwise, resolves to an Option.Some with the value.

fn

Declares a promise that must return an Option, returning a new function that returns an OptionAsync and never rejects.

fromOption

Converts an OptionAsync from an Option.

fromResult

Converts an OptionAsync from a Result.

fromResultAsync

Converts an OptionAsync from a ResultAsync.

resource

Wraps a resource and provides a safe way to use it with error handling.

predicate

Returns a function that asserts that a value passes the test implemented by the provided function. Can create an OptionAsync that resolves to either Some with a narrowed type or None.

Combinators

values

Given an array of OptionAsyncs, returns an array containing only the values inside Some.

zip

Combines two OptionAsyncs into a single OptionAsync containing a tuple of their values, if both OptionAsyncs are Some variants, otherwise, returns None.

zipWith

Combines two OptionAsyncs into a single OptionAsync. The new value is produced by applying the given function to both values, if both OptionAsyncs are Some variants, otherwise, returns None.

Conversions

then

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

match

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

unwrap

triangle-exclamation

Returns a promise that unwraps the underlying Option value.

unwrapOr

Returns a promise that unwraps the underlying Option value.

If the promise resolves to an Option.None, returns the result of the provided callback.

unwrapOrNull

Returns a promise that unwraps the value of the underlying Option if it is an Option.Some, otherwise returns null.

unwrapOrUndefined

Returns a promise that unwraps the value of the underlying Option if it is an Option.Some, otherwise returns undefined.

expect

triangle-exclamation

Returns a promise that unwraps the underlying Option value.

contains

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

toArray

Returns a Promise that converts the underlying Option to an array.

Transformations

map

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

andThen

Applies a callback function to the value of the OptionAsync when it is Some, and returns the new value. Can work with both Option and OptionAsync returns.

filter

Asserts that the OptionAsync value passes the test implemented by the provided function. Can narrow types.

Fallbacks

or

Replaces the current OptionAsync with the provided fallback OptionAsync when it is None.

firstSomeOf

Resolves to the first OptionAsync.Some value in the iterable. If all values are OptionAsync.None, resolves to None.

Comparisons

isOptionAsync

Asserts that an unknown value is an OptionAsync.

Other

tap

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

Last updated

Was this helpful?