OptionAsyncExtensions Class
Definition
Provides Task-based asynchronous companions to the Option<T> railway combinators.
public static class OptionAsyncExtensions
- Inheritance
-
OptionAsyncExtensions
- Inherited Members
Remarks
Each operation mirrors its synchronous counterpart - MapAsync pairs with
Map<TResult>(Func<T, TResult>), BindAsync with
Bind<TResult>(Func<T, Option<TResult>>), and MatchAsync with
Match<TResult>(Func<T, TResult>, Func<TResult>) - so an asynchronous pipeline over
Task<Option<T>> composes exactly like its synchronous equivalent.
Argument validation runs synchronously, so a null source task or delegate faults at the call site rather than on the returned task.
The combinators accept no CancellationToken: they perform no I/O of their own, so cancellation belongs to the caller's delegates, which can close over their own tokens.
Methods
BindAsync<T, TResult>(Option<T>, Func<T, Task<Option<TResult>>>)
Asynchronously projects the contained value into another option using the specified asynchronous option-returning selector.
public static Task<Option<TResult>> BindAsync<T, TResult>(this Option<T> source, Func<T, Task<Option<TResult>>> selector)
Parameters
sourceOption<T>The option to project.
selectorFunc<T, Task<Option<TResult>>>The asynchronous option-returning projection applied to the contained value.
Returns
- Task<Option<TResult>>
A task that completes with the option produced by the awaited selector when the option carries a value; otherwise
None.
Type Parameters
TThe value type of the source option.
TResultThe value type of the resulting option.
Remarks
The selector is not invoked when the option is None. Argument validation runs synchronously, so a
null selector faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
selectoris null.
BindAsync<T, TResult>(Task<Option<T>>, Func<T, Option<TResult>>)
Asynchronously projects the value contained by the awaited option into another option using the specified option-returning selector.
public static Task<Option<TResult>> BindAsync<T, TResult>(this Task<Option<T>> source, Func<T, Option<TResult>> selector)
Parameters
sourceTask<Option<T>>The task producing the option to project. Must not be null.
selectorFunc<T, Option<TResult>>The option-returning projection applied to the contained value.
Returns
- Task<Option<TResult>>
A task that completes with the option produced by the selector when the awaited option carries a value; otherwise
None.
Type Parameters
TThe value type of the source option.
TResultThe value type of the resulting option.
Remarks
The selector is not invoked when the awaited option is None. Argument validation runs synchronously, so a
null argument faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
sourceorselectoris null.
BindAsync<T, TResult>(Task<Option<T>>, Func<T, Task<Option<TResult>>>)
Asynchronously projects the value contained by the awaited option into another option using the specified asynchronous option-returning selector.
public static Task<Option<TResult>> BindAsync<T, TResult>(this Task<Option<T>> source, Func<T, Task<Option<TResult>>> selector)
Parameters
sourceTask<Option<T>>The task producing the option to project. Must not be null.
selectorFunc<T, Task<Option<TResult>>>The asynchronous option-returning projection applied to the contained value.
Returns
- Task<Option<TResult>>
A task that completes with the option produced by the awaited selector when the awaited option carries a value; otherwise
None.
Type Parameters
TThe value type of the source option.
TResultThe value type of the resulting option.
Remarks
The selector is not invoked when the awaited option is None. Argument validation runs synchronously, so a
null argument faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
sourceorselectoris null.
MapAsync<T, TResult>(Option<T>, Func<T, Task<TResult>>)
Asynchronously projects the contained value using the specified asynchronous selector.
public static Task<Option<TResult>> MapAsync<T, TResult>(this Option<T> source, Func<T, Task<TResult>> selector)
Parameters
sourceOption<T>The option to project.
selectorFunc<T, Task<TResult>>The asynchronous projection applied to the contained value.
Returns
- Task<Option<TResult>>
A task that completes with
Some(await selector(value))when the option carries a value and the awaited projection is non-null; otherwiseNone.
Type Parameters
TThe value type of the source option.
TResultThe type produced by the selector.
Remarks
The selector is not invoked when the option is None. A null awaited projection result
maps to None (the lenient lift), matching Map<TResult>(Func<T, TResult>).
Argument validation runs synchronously, so a null selector faults at the call site rather
than on the returned task.
Exceptions
- ArgumentNullException
selectoris null.
MapAsync<T, TResult>(Task<Option<T>>, Func<T, Task<TResult>>)
Asynchronously projects the value contained by the awaited option using the specified asynchronous selector.
public static Task<Option<TResult>> MapAsync<T, TResult>(this Task<Option<T>> source, Func<T, Task<TResult>> selector)
Parameters
sourceTask<Option<T>>The task producing the option to project. Must not be null.
selectorFunc<T, Task<TResult>>The asynchronous projection applied to the contained value.
Returns
- Task<Option<TResult>>
A task that completes with
Some(await selector(value))when the awaited option carries a value and the awaited projection is non-null; otherwiseNone.
Type Parameters
TThe value type of the source option.
TResultThe type produced by the selector.
Remarks
The selector is not invoked when the awaited option is None. A null awaited projection
result maps to None (the lenient lift), matching Map<TResult>(Func<T, TResult>).
Argument validation runs synchronously, so a null argument faults at the call site rather
than on the returned task.
Exceptions
- ArgumentNullException
sourceorselectoris null.
MapAsync<T, TResult>(Task<Option<T>>, Func<T, TResult>)
Asynchronously projects the value contained by the awaited option using the specified selector.
public static Task<Option<TResult>> MapAsync<T, TResult>(this Task<Option<T>> source, Func<T, TResult> selector)
Parameters
sourceTask<Option<T>>The task producing the option to project. Must not be null.
selectorFunc<T, TResult>The projection applied to the contained value.
Returns
- Task<Option<TResult>>
A task that completes with
Some(selector(value))when the awaited option carries a value and the projection is non-null; otherwiseNone.
Type Parameters
TThe value type of the source option.
TResultThe type produced by the selector.
Remarks
The selector is not invoked when the awaited option is None. A null projection result
maps to None (the lenient lift), matching Map<TResult>(Func<T, TResult>).
Argument validation runs synchronously, so a null argument faults at the call site rather
than on the returned task.
Exceptions
- ArgumentNullException
sourceorselectoris null.
MatchAsync<T, TResult>(Option<T>, Func<T, Task<TResult>>, Func<Task<TResult>>)
Asynchronously collapses the option to a single value by invoking and awaiting the matching asynchronous branch.
public static Task<TResult> MatchAsync<T, TResult>(this Option<T> source, Func<T, Task<TResult>> onSome, Func<Task<TResult>> onNone)
Parameters
sourceOption<T>The option to collapse.
onSomeFunc<T, Task<TResult>>The asynchronous projection invoked with the contained value when one is present.
onNoneFunc<Task<TResult>>The asynchronous factory invoked when no value is present.
Returns
- Task<TResult>
A task that completes with the value produced by the awaited branch.
Type Parameters
TThe value type of the source option.
TResultThe type produced by both branches.
Remarks
Exactly one branch is invoked. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
onSomeoronNoneis null.
MatchAsync<T, TResult>(Task<Option<T>>, Func<T, Task<TResult>>, Func<Task<TResult>>)
Asynchronously collapses the awaited option to a single value by invoking and awaiting the matching asynchronous branch.
public static Task<TResult> MatchAsync<T, TResult>(this Task<Option<T>> source, Func<T, Task<TResult>> onSome, Func<Task<TResult>> onNone)
Parameters
sourceTask<Option<T>>The task producing the option to collapse. Must not be null.
onSomeFunc<T, Task<TResult>>The asynchronous projection invoked with the contained value when one is present.
onNoneFunc<Task<TResult>>The asynchronous factory invoked when no value is present.
Returns
- Task<TResult>
A task that completes with the value produced by the awaited branch.
Type Parameters
TThe value type of the source option.
TResultThe type produced by both branches.
Remarks
Exactly one branch is invoked. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
source,onSome, oronNoneis null.
MatchAsync<T, TResult>(Task<Option<T>>, Func<T, TResult>, Func<TResult>)
Asynchronously collapses the awaited option to a single value by invoking the matching branch.
public static Task<TResult> MatchAsync<T, TResult>(this Task<Option<T>> source, Func<T, TResult> onSome, Func<TResult> onNone)
Parameters
sourceTask<Option<T>>The task producing the option to collapse. Must not be null.
onSomeFunc<T, TResult>The projection invoked with the contained value when one is present.
onNoneFunc<TResult>The factory invoked when no value is present.
Returns
- Task<TResult>
A task that completes with the value produced by the invoked branch.
Type Parameters
TThe value type of the source option.
TResultThe type produced by both branches.
Remarks
Exactly one branch is invoked. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
source,onSome, oronNoneis null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |