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.
Constructs an AsyncOption that resolves to a NoneOption.
fromNullable
Constructs an AsyncOption 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 AsyncOption from a falsy value.
If the value is falsy, resolves to a None. Otherwise, resolves to a Some with the value.
try
Constructs an AsyncOption 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.
promise
Constructs an AsyncOption from a Promise that returns an Option, and never rejects.
liftPromise
Lifts a Promise that resolves to an Option or nullable value to a function that returns an AsyncOption.
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.
Combinators
values
Given an array of AsyncOptions, returns an array containing only the values inside Some.
zip
Combines two AsyncOptions into a single AsyncOption containing a tuple of their values, if both AsyncOptions are Some variants, otherwise, returns None.
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.
Conversions
then
Attaches a callback for the resolution of the Promise inside the AsyncOption.
match
Returns a promise that compares the underlying Option against the possible patterns, and then execute code based on which pattern matches.
unwrap
Rejects the promise with UnwrapError if the Option is None.
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
Rejects the promise with the provided Error if the Option is None.
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.
toAsyncResult
Converts the AsyncOption to an AsyncResult. Can provide custom error handling.
toArray
Returns a Promise that converts the underlying Option to an array.
Transformations
map
Applies a callback function to the value of the AsyncOption when it is Some, returning a new AsyncOption containing the new value.
andThen
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.
filter
Asserts that the AsyncOption value passes the test implemented by the provided function. Can narrow types.
Fallbacks
or
Replaces the current AsyncOption with the provided fallback AsyncOption when it is None.
firstSomeOf
Resolves to the first AsyncOption.Some value in the iterable. If all values are AsyncOption.None, resolves to None.
Comparisons
is
Asserts that an unknown value is an AsyncOption.
Other
tap
Calls the function with the AsyncOption value, then returns the AsyncOption itself. The return value of the provided function is ignored.