EitherAsyncExtensions Class
Definition
Provides Task-based asynchronous companions to the Either<TLeft, TRight> combinators.
public static class EitherAsyncExtensions
- Inheritance
-
EitherAsyncExtensions
- Inherited Members
Remarks
Each operation mirrors its synchronous counterpart - MapLeftAsync pairs with
MapLeft<TResult>(Func<TLeft, TResult>), MapRightAsync with
MapRight<TResult>(Func<TRight, TResult>), and MatchAsync with
Match<TResult>(Func<TLeft, TResult>, Func<TRight, TResult>) - so an
asynchronous pipeline over Task<Either<TLeft, TRight>> 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.
Every combinator preserves the uninitialized-instance contract of the underlying type: operating on
default(Either<TLeft, TRight>) throws InvalidOperationException, matching the
synchronous combinators.
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
MapLeftAsync<TLeft, TRight, TResult>(Either<TLeft, TRight>, Func<TLeft, Task<TResult>>)
Asynchronously projects the left value using the specified asynchronous selector, passing a right value through untouched.
public static Task<Either<TResult, TRight>> MapLeftAsync<TLeft, TRight, TResult>(this Either<TLeft, TRight> source, Func<TLeft, Task<TResult>> selector)
Parameters
sourceEither<TLeft, TRight>The either to project.
selectorFunc<TLeft, Task<TResult>>The asynchronous projection applied to the left value when the left side is active.
Returns
- Task<Either<TResult, TRight>>
A task that completes with
Left(await selector(value))when the left side is active; otherwise a right-carrying either with the original right value.
Type Parameters
TLeftThe type of the value carried on the left side.
TRightThe type of the value carried on the right side.
TResultThe left type produced by the selector.
Remarks
The selector is not invoked when the right side is active. The lift is strict: a null awaited projection result is rejected by the Left(TLeft) factory. Argument validation runs synchronously, so a null selector faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
- InvalidOperationException
The either is
default(Either<TLeft, TRight>)and carries neither side.
MapLeftAsync<TLeft, TRight, TResult>(Task<Either<TLeft, TRight>>, Func<TLeft, Task<TResult>>)
Asynchronously projects the left value of the awaited either using the specified asynchronous selector, passing a right value through untouched.
public static Task<Either<TResult, TRight>> MapLeftAsync<TLeft, TRight, TResult>(this Task<Either<TLeft, TRight>> source, Func<TLeft, Task<TResult>> selector)
Parameters
sourceTask<Either<TLeft, TRight>>The task producing the either to project. Must not be null.
selectorFunc<TLeft, Task<TResult>>The asynchronous projection applied to the left value when the left side is active.
Returns
- Task<Either<TResult, TRight>>
A task that completes with
Left(await selector(value))when the left side is active; otherwise a right-carrying either with the original right value.
Type Parameters
TLeftThe type of the value carried on the left side.
TRightThe type of the value carried on the right side.
TResultThe left type produced by the selector.
Remarks
The selector is not invoked when the right side is active. The lift is strict: a null awaited projection result is rejected by the Left(TLeft) factory. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
sourceorselectoris null, or the awaited projection produced null.- InvalidOperationException
The awaited either is
default(Either<TLeft, TRight>)and carries neither side.
MapLeftAsync<TLeft, TRight, TResult>(Task<Either<TLeft, TRight>>, Func<TLeft, TResult>)
Asynchronously projects the left value of the awaited either using the specified selector, passing a right value through untouched.
public static Task<Either<TResult, TRight>> MapLeftAsync<TLeft, TRight, TResult>(this Task<Either<TLeft, TRight>> source, Func<TLeft, TResult> selector)
Parameters
sourceTask<Either<TLeft, TRight>>The task producing the either to project. Must not be null.
selectorFunc<TLeft, TResult>The projection applied to the left value when the left side is active.
Returns
- Task<Either<TResult, TRight>>
A task that completes with
Left(selector(value))when the left side is active; otherwise a right-carrying either with the original right value.
Type Parameters
TLeftThe type of the value carried on the left side.
TRightThe type of the value carried on the right side.
TResultThe left type produced by the selector.
Remarks
The selector is not invoked when the right side is active. The lift is strict, matching MapLeft<TResult>(Func<TLeft, TResult>). Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
sourceorselectoris null, or the projection returned null.- InvalidOperationException
The awaited either is
default(Either<TLeft, TRight>)and carries neither side.
MapRightAsync<TLeft, TRight, TResult>(Either<TLeft, TRight>, Func<TRight, Task<TResult>>)
Asynchronously projects the right value using the specified asynchronous selector, passing a left value through untouched.
public static Task<Either<TLeft, TResult>> MapRightAsync<TLeft, TRight, TResult>(this Either<TLeft, TRight> source, Func<TRight, Task<TResult>> selector)
Parameters
sourceEither<TLeft, TRight>The either to project.
selectorFunc<TRight, Task<TResult>>The asynchronous projection applied to the right value when the right side is active.
Returns
- Task<Either<TLeft, TResult>>
A task that completes with
Right(await selector(value))when the right side is active; otherwise a left-carrying either with the original left value.
Type Parameters
TLeftThe type of the value carried on the left side.
TRightThe type of the value carried on the right side.
TResultThe right type produced by the selector.
Remarks
The selector is not invoked when the left side is active. The lift is strict: a null awaited projection result is rejected by the Right(TRight) factory. Argument validation runs synchronously, so a null selector faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
- InvalidOperationException
The either is
default(Either<TLeft, TRight>)and carries neither side.
MapRightAsync<TLeft, TRight, TResult>(Task<Either<TLeft, TRight>>, Func<TRight, Task<TResult>>)
Asynchronously projects the right value of the awaited either using the specified asynchronous selector, passing a left value through untouched.
public static Task<Either<TLeft, TResult>> MapRightAsync<TLeft, TRight, TResult>(this Task<Either<TLeft, TRight>> source, Func<TRight, Task<TResult>> selector)
Parameters
sourceTask<Either<TLeft, TRight>>The task producing the either to project. Must not be null.
selectorFunc<TRight, Task<TResult>>The asynchronous projection applied to the right value when the right side is active.
Returns
- Task<Either<TLeft, TResult>>
A task that completes with
Right(await selector(value))when the right side is active; otherwise a left-carrying either with the original left value.
Type Parameters
TLeftThe type of the value carried on the left side.
TRightThe type of the value carried on the right side.
TResultThe right type produced by the selector.
Remarks
The selector is not invoked when the left side is active. The lift is strict: a null awaited projection result is rejected by the Right(TRight) factory. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
sourceorselectoris null, or the awaited projection produced null.- InvalidOperationException
The awaited either is
default(Either<TLeft, TRight>)and carries neither side.
MapRightAsync<TLeft, TRight, TResult>(Task<Either<TLeft, TRight>>, Func<TRight, TResult>)
Asynchronously projects the right value of the awaited either using the specified selector, passing a left value through untouched.
public static Task<Either<TLeft, TResult>> MapRightAsync<TLeft, TRight, TResult>(this Task<Either<TLeft, TRight>> source, Func<TRight, TResult> selector)
Parameters
sourceTask<Either<TLeft, TRight>>The task producing the either to project. Must not be null.
selectorFunc<TRight, TResult>The projection applied to the right value when the right side is active.
Returns
- Task<Either<TLeft, TResult>>
A task that completes with
Right(selector(value))when the right side is active; otherwise a left-carrying either with the original left value.
Type Parameters
TLeftThe type of the value carried on the left side.
TRightThe type of the value carried on the right side.
TResultThe right type produced by the selector.
Remarks
The selector is not invoked when the left side is active. The lift is strict, matching MapRight<TResult>(Func<TRight, TResult>). Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
sourceorselectoris null, or the projection returned null.- InvalidOperationException
The awaited either is
default(Either<TLeft, TRight>)and carries neither side.
MatchAsync<TLeft, TRight, TResult>(Either<TLeft, TRight>, Func<TLeft, Task<TResult>>, Func<TRight, Task<TResult>>)
Asynchronously collapses the either to a single value by invoking and awaiting the asynchronous branch for the active side.
public static Task<TResult> MatchAsync<TLeft, TRight, TResult>(this Either<TLeft, TRight> source, Func<TLeft, Task<TResult>> onLeft, Func<TRight, Task<TResult>> onRight)
Parameters
sourceEither<TLeft, TRight>The either to collapse.
onLeftFunc<TLeft, Task<TResult>>The asynchronous projection invoked with the left value when the left side is active.
onRightFunc<TRight, Task<TResult>>The asynchronous projection invoked with the right value when the right side is active.
Returns
- Task<TResult>
A task that completes with the value produced by the awaited branch.
Type Parameters
TLeftThe type of the value carried on the left side.
TRightThe type of the value carried on the right side.
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
onLeftoronRightis null.- InvalidOperationException
The either is
default(Either<TLeft, TRight>)and carries neither side.
MatchAsync<TLeft, TRight, TResult>(Task<Either<TLeft, TRight>>, Func<TLeft, Task<TResult>>, Func<TRight, Task<TResult>>)
Asynchronously collapses the awaited either to a single value by invoking and awaiting the asynchronous branch for the active side.
public static Task<TResult> MatchAsync<TLeft, TRight, TResult>(this Task<Either<TLeft, TRight>> source, Func<TLeft, Task<TResult>> onLeft, Func<TRight, Task<TResult>> onRight)
Parameters
sourceTask<Either<TLeft, TRight>>The task producing the either to collapse. Must not be null.
onLeftFunc<TLeft, Task<TResult>>The asynchronous projection invoked with the left value when the left side is active.
onRightFunc<TRight, Task<TResult>>The asynchronous projection invoked with the right value when the right side is active.
Returns
- Task<TResult>
A task that completes with the value produced by the awaited branch.
Type Parameters
TLeftThe type of the value carried on the left side.
TRightThe type of the value carried on the right side.
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,onLeft, oronRightis null.- InvalidOperationException
The awaited either is
default(Either<TLeft, TRight>)and carries neither side.
MatchAsync<TLeft, TRight, TResult>(Task<Either<TLeft, TRight>>, Func<TLeft, TResult>, Func<TRight, TResult>)
Asynchronously collapses the awaited either to a single value by invoking the branch for the active side.
public static Task<TResult> MatchAsync<TLeft, TRight, TResult>(this Task<Either<TLeft, TRight>> source, Func<TLeft, TResult> onLeft, Func<TRight, TResult> onRight)
Parameters
sourceTask<Either<TLeft, TRight>>The task producing the either to collapse. Must not be null.
onLeftFunc<TLeft, TResult>The projection invoked with the left value when the left side is active.
onRightFunc<TRight, TResult>The projection invoked with the right value when the right side is active.
Returns
- Task<TResult>
A task that completes with the value produced by the invoked branch.
Type Parameters
TLeftThe type of the value carried on the left side.
TRightThe type of the value carried on the right side.
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,onLeft, oronRightis null.- InvalidOperationException
The awaited either is
default(Either<TLeft, TRight>)and carries neither side.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |