ResultAsyncExtensions Class
Definition
Provides Task-based asynchronous companions to the Result<T> and non-generic Result railway combinators.
public static class ResultAsyncExtensions
- Inheritance
-
ResultAsyncExtensions
- Inherited Members
Remarks
Each operation mirrors its synchronous counterpart - MapAsync pairs with
Map<TResult>(Func<T, TResult>), BindAsync with
Bind<TResult>(Func<T, Result<TResult>>), MatchAsync with
Match<TResult>(Func<T, TResult>, Func<ResultError, TResult>), and TapAsync /
TapErrorAsync with Tap(Action<T>) and
TapError(Action<ResultError>) - so an asynchronous pipeline over
Task<Result<T>> composes exactly like its synchronous equivalent, with the error propagating
untouched past every combinator.
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>(Result<T>, Func<T, Task<Result<TResult>>>)
Asynchronously projects the carried value into another result using the specified asynchronous result-returning selector.
public static Task<Result<TResult>> BindAsync<T, TResult>(this Result<T> source, Func<T, Task<Result<TResult>>> selector)
Parameters
sourceResult<T>The result to project.
selectorFunc<T, Task<Result<TResult>>>The asynchronous result-returning projection applied to the carried value.
Returns
- Task<Result<TResult>>
A task that completes with the result produced by the awaited selector when the result represents success; otherwise a failure carrying the original error.
Type Parameters
TThe value type of the source result.
TResultThe value type of the resulting result.
Remarks
The selector is not invoked when the result is a failure. 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<Result<T>>, Func<T, Result<TResult>>)
Asynchronously projects the value carried by the awaited result into another result using the specified result-returning selector.
public static Task<Result<TResult>> BindAsync<T, TResult>(this Task<Result<T>> source, Func<T, Result<TResult>> selector)
Parameters
sourceTask<Result<T>>The task producing the result to project. Must not be null.
selectorFunc<T, Result<TResult>>The result-returning projection applied to the carried value.
Returns
- Task<Result<TResult>>
A task that completes with the result produced by the selector when the awaited result represents success; otherwise a failure carrying the original error.
Type Parameters
TThe value type of the source result.
TResultThe value type of the resulting result.
Remarks
The selector is not invoked when the awaited result is a failure. 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<Result<T>>, Func<T, Task<Result<TResult>>>)
Asynchronously projects the value carried by the awaited result into another result using the specified asynchronous result-returning selector.
public static Task<Result<TResult>> BindAsync<T, TResult>(this Task<Result<T>> source, Func<T, Task<Result<TResult>>> selector)
Parameters
sourceTask<Result<T>>The task producing the result to project. Must not be null.
selectorFunc<T, Task<Result<TResult>>>The asynchronous result-returning projection applied to the carried value.
Returns
- Task<Result<TResult>>
A task that completes with the result produced by the awaited selector when the awaited result represents success; otherwise a failure carrying the original error.
Type Parameters
TThe value type of the source result.
TResultThe value type of the resulting result.
Remarks
The selector is not invoked when the awaited result is a failure. 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>(Result<T>, Func<T, Task<TResult>>)
Asynchronously projects the carried value using the specified asynchronous selector.
public static Task<Result<TResult>> MapAsync<T, TResult>(this Result<T> source, Func<T, Task<TResult>> selector)
Parameters
sourceResult<T>The result to project.
selectorFunc<T, Task<TResult>>The asynchronous projection applied to the carried value.
Returns
- Task<Result<TResult>>
A task that completes with
Success(await selector(value))when the result represents success; otherwise a failure carrying the original error.
Type Parameters
TThe value type of the source result.
TResultThe type produced by the selector.
Remarks
The selector is not invoked when the result is a failure. The projected value is lifted through the strict Success<T>(T) factory, so a null awaited projection result throws ArgumentNullException, 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
MapAsync<T, TResult>(Task<Result<T>>, Func<T, Task<TResult>>)
Asynchronously projects the value carried by the awaited result using the specified asynchronous selector.
public static Task<Result<TResult>> MapAsync<T, TResult>(this Task<Result<T>> source, Func<T, Task<TResult>> selector)
Parameters
sourceTask<Result<T>>The task producing the result to project. Must not be null.
selectorFunc<T, Task<TResult>>The asynchronous projection applied to the carried value.
Returns
- Task<Result<TResult>>
A task that completes with
Success(await selector(value))when the awaited result represents success; otherwise a failure carrying the original error.
Type Parameters
TThe value type of the source result.
TResultThe type produced by the selector.
Remarks
The selector is not invoked when the awaited result is a failure. The projected value is lifted through the strict Success<T>(T) factory, so a null awaited projection result throws ArgumentNullException, 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, or the awaited projection produced null.
MapAsync<T, TResult>(Task<Result<T>>, Func<T, TResult>)
Asynchronously projects the value carried by the awaited result using the specified selector.
public static Task<Result<TResult>> MapAsync<T, TResult>(this Task<Result<T>> source, Func<T, TResult> selector)
Parameters
sourceTask<Result<T>>The task producing the result to project. Must not be null.
selectorFunc<T, TResult>The projection applied to the carried value.
Returns
- Task<Result<TResult>>
A task that completes with
Success(selector(value))when the awaited result represents success; otherwise a failure carrying the original error.
Type Parameters
TThe value type of the source result.
TResultThe type produced by the selector.
Remarks
The selector is not invoked when the awaited result is a failure. The projected value is lifted through the strict Success<T>(T) factory, so a null projection result throws ArgumentNullException, 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, or the projection returned null.
MatchAsync<TResult>(Result, Func<Task<TResult>>, Func<ResultError, Task<TResult>>)
Asynchronously collapses the valueless result to a single value by invoking and awaiting the matching asynchronous branch.
public static Task<TResult> MatchAsync<TResult>(this Result source, Func<Task<TResult>> onSuccess, Func<ResultError, Task<TResult>> onFailure)
Parameters
sourceResultThe result to collapse.
onSuccessFunc<Task<TResult>>The asynchronous factory invoked when the result represents success.
onFailureFunc<ResultError, Task<TResult>>The asynchronous projection invoked with the carried error when the result represents failure.
Returns
- Task<TResult>
A task that completes with the value produced by the awaited branch.
Type Parameters
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
onSuccessoronFailureis null.
MatchAsync<TResult>(Task<Result>, Func<Task<TResult>>, Func<ResultError, Task<TResult>>)
Asynchronously collapses the awaited valueless result to a single value by invoking and awaiting the matching asynchronous branch.
public static Task<TResult> MatchAsync<TResult>(this Task<Result> source, Func<Task<TResult>> onSuccess, Func<ResultError, Task<TResult>> onFailure)
Parameters
sourceTask<Result>The task producing the result to collapse. Must not be null.
onSuccessFunc<Task<TResult>>The asynchronous factory invoked when the result represents success.
onFailureFunc<ResultError, Task<TResult>>The asynchronous projection invoked with the carried error when the result represents failure.
Returns
- Task<TResult>
A task that completes with the value produced by the awaited branch.
Type Parameters
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,onSuccess, oronFailureis null.
MatchAsync<TResult>(Task<Result>, Func<TResult>, Func<ResultError, TResult>)
Asynchronously collapses the awaited valueless result to a single value by invoking the matching branch.
public static Task<TResult> MatchAsync<TResult>(this Task<Result> source, Func<TResult> onSuccess, Func<ResultError, TResult> onFailure)
Parameters
sourceTask<Result>The task producing the result to collapse. Must not be null.
onSuccessFunc<TResult>The factory invoked when the result represents success.
onFailureFunc<ResultError, TResult>The projection invoked with the carried error when the result represents failure.
Returns
- Task<TResult>
A task that completes with the value produced by the invoked branch.
Type Parameters
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,onSuccess, oronFailureis null.
MatchAsync<T, TResult>(Result<T>, Func<T, Task<TResult>>, Func<ResultError, Task<TResult>>)
Asynchronously collapses the result to a single value by invoking and awaiting the matching asynchronous branch.
public static Task<TResult> MatchAsync<T, TResult>(this Result<T> source, Func<T, Task<TResult>> onSuccess, Func<ResultError, Task<TResult>> onFailure)
Parameters
sourceResult<T>The result to collapse.
onSuccessFunc<T, Task<TResult>>The asynchronous projection invoked with the carried value when the result represents success.
onFailureFunc<ResultError, Task<TResult>>The asynchronous projection invoked with the carried error when the result represents failure.
Returns
- Task<TResult>
A task that completes with the value produced by the awaited branch.
Type Parameters
TThe value type of the source result.
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
onSuccessoronFailureis null.
MatchAsync<T, TResult>(Task<Result<T>>, Func<T, Task<TResult>>, Func<ResultError, Task<TResult>>)
Asynchronously collapses the awaited result to a single value by invoking and awaiting the matching asynchronous branch.
public static Task<TResult> MatchAsync<T, TResult>(this Task<Result<T>> source, Func<T, Task<TResult>> onSuccess, Func<ResultError, Task<TResult>> onFailure)
Parameters
sourceTask<Result<T>>The task producing the result to collapse. Must not be null.
onSuccessFunc<T, Task<TResult>>The asynchronous projection invoked with the carried value when the result represents success.
onFailureFunc<ResultError, Task<TResult>>The asynchronous projection invoked with the carried error when the result represents failure.
Returns
- Task<TResult>
A task that completes with the value produced by the awaited branch.
Type Parameters
TThe value type of the source result.
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,onSuccess, oronFailureis null.
MatchAsync<T, TResult>(Task<Result<T>>, Func<T, TResult>, Func<ResultError, TResult>)
Asynchronously collapses the awaited result to a single value by invoking the matching branch.
public static Task<TResult> MatchAsync<T, TResult>(this Task<Result<T>> source, Func<T, TResult> onSuccess, Func<ResultError, TResult> onFailure)
Parameters
sourceTask<Result<T>>The task producing the result to collapse. Must not be null.
onSuccessFunc<T, TResult>The projection invoked with the carried value when the result represents success.
onFailureFunc<ResultError, TResult>The projection invoked with the carried error when the result represents failure.
Returns
- Task<TResult>
A task that completes with the value produced by the invoked branch.
Type Parameters
TThe value type of the source result.
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,onSuccess, oronFailureis null.
TapAsync<T>(Result<T>, Func<T, Task>)
Asynchronously invokes and awaits the specified asynchronous callback with the carried value when the result represents success.
public static Task<Result<T>> TapAsync<T>(this Result<T> source, Func<T, Task> onSuccess)
Parameters
sourceResult<T>The result to observe.
onSuccessFunc<T, Task>The asynchronous side effect invoked with the carried value.
Returns
Type Parameters
TThe value type of the source result.
Remarks
The callback is not invoked when the result is a failure; the result itself is always returned, and the callback is awaited before the returned task completes. Argument validation runs synchronously, so a null callback faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
onSuccessis null.
TapAsync<T>(Task<Result<T>>, Action<T>)
Asynchronously invokes the specified action with the carried value when the awaited result represents success.
public static Task<Result<T>> TapAsync<T>(this Task<Result<T>> source, Action<T> onSuccess)
Parameters
sourceTask<Result<T>>The task producing the result to observe. Must not be null.
onSuccessAction<T>The side effect invoked with the carried value.
Returns
Type Parameters
TThe value type of the source result.
Remarks
The action is not invoked when the awaited result is a failure; the result itself is always returned. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
sourceoronSuccessis null.
TapAsync<T>(Task<Result<T>>, Func<T, Task>)
Asynchronously invokes and awaits the specified asynchronous callback with the carried value when the awaited result represents success.
public static Task<Result<T>> TapAsync<T>(this Task<Result<T>> source, Func<T, Task> onSuccess)
Parameters
sourceTask<Result<T>>The task producing the result to observe. Must not be null.
onSuccessFunc<T, Task>The asynchronous side effect invoked with the carried value.
Returns
Type Parameters
TThe value type of the source result.
Remarks
The callback is not invoked when the awaited result is a failure; the result itself is always returned, and the callback is awaited before the returned task completes. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
sourceoronSuccessis null.
TapErrorAsync<T>(Result<T>, Func<ResultError, Task>)
Asynchronously invokes and awaits the specified asynchronous callback with the carried error when the result represents failure.
public static Task<Result<T>> TapErrorAsync<T>(this Result<T> source, Func<ResultError, Task> onFailure)
Parameters
sourceResult<T>The result to observe.
onFailureFunc<ResultError, Task>The asynchronous side effect invoked with the carried error.
Returns
Type Parameters
TThe value type of the source result.
Remarks
The callback is not invoked when the result is a success; the result itself is always returned, and the callback is awaited before the returned task completes. Argument validation runs synchronously, so a null callback faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
onFailureis null.
TapErrorAsync<T>(Task<Result<T>>, Action<ResultError>)
Asynchronously invokes the specified action with the carried error when the awaited result represents failure.
public static Task<Result<T>> TapErrorAsync<T>(this Task<Result<T>> source, Action<ResultError> onFailure)
Parameters
sourceTask<Result<T>>The task producing the result to observe. Must not be null.
onFailureAction<ResultError>The side effect invoked with the carried error.
Returns
Type Parameters
TThe value type of the source result.
Remarks
The action is not invoked when the awaited result is a success; the result itself is always returned. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
sourceoronFailureis null.
TapErrorAsync<T>(Task<Result<T>>, Func<ResultError, Task>)
Asynchronously invokes and awaits the specified asynchronous callback with the carried error when the awaited result represents failure.
public static Task<Result<T>> TapErrorAsync<T>(this Task<Result<T>> source, Func<ResultError, Task> onFailure)
Parameters
sourceTask<Result<T>>The task producing the result to observe. Must not be null.
onFailureFunc<ResultError, Task>The asynchronous side effect invoked with the carried error.
Returns
Type Parameters
TThe value type of the source result.
Remarks
The callback is not invoked when the awaited result is a success; the result itself is always returned, and the callback is awaited before the returned task completes. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.
Exceptions
- ArgumentNullException
sourceoronFailureis null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |