Table of Contents

ResultAsyncExtensions Class

Definition

Namespace
Bodu.Functional
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
ResultAsyncExtensions.BindAsync.cs

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

source Result<T>

The result to project.

selector Func<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

T

The value type of the source result.

TResult

The 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

selector is 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

source Task<Result<T>>

The task producing the result to project. Must not be null.

selector Func<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

T

The value type of the source result.

TResult

The 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

source or selector is 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

source Task<Result<T>>

The task producing the result to project. Must not be null.

selector Func<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

T

The value type of the source result.

TResult

The 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

source or selector is 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

source Result<T>

The result to project.

selector Func<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

T

The value type of the source result.

TResult

The 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

ArgumentNullException

selector is null, or the awaited projection produced null.

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

source Task<Result<T>>

The task producing the result to project. Must not be null.

selector Func<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

T

The value type of the source result.

TResult

The 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

source or selector is 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

source Task<Result<T>>

The task producing the result to project. Must not be null.

selector Func<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

T

The value type of the source result.

TResult

The 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

source or selector is 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

source Result

The result to collapse.

onSuccess Func<Task<TResult>>

The asynchronous factory invoked when the result represents success.

onFailure Func<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

TResult

The 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

onSuccess or onFailure is 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

source Task<Result>

The task producing the result to collapse. Must not be null.

onSuccess Func<Task<TResult>>

The asynchronous factory invoked when the result represents success.

onFailure Func<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

TResult

The 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, or onFailure is 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

source Task<Result>

The task producing the result to collapse. Must not be null.

onSuccess Func<TResult>

The factory invoked when the result represents success.

onFailure Func<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

TResult

The 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, or onFailure is 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

source Result<T>

The result to collapse.

onSuccess Func<T, Task<TResult>>

The asynchronous projection invoked with the carried value when the result represents success.

onFailure Func<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

T

The value type of the source result.

TResult

The 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

onSuccess or onFailure is 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

source Task<Result<T>>

The task producing the result to collapse. Must not be null.

onSuccess Func<T, Task<TResult>>

The asynchronous projection invoked with the carried value when the result represents success.

onFailure Func<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

T

The value type of the source result.

TResult

The 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, or onFailure is 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

source Task<Result<T>>

The task producing the result to collapse. Must not be null.

onSuccess Func<T, TResult>

The projection invoked with the carried value when the result represents success.

onFailure Func<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

T

The value type of the source result.

TResult

The 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, or onFailure is 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

source Result<T>

The result to observe.

onSuccess Func<T, Task>

The asynchronous side effect invoked with the carried value.

Returns

Task<Result<T>>

A task that completes with this result, unchanged, so calls can be chained.

Type Parameters

T

The 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

onSuccess is 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

source Task<Result<T>>

The task producing the result to observe. Must not be null.

onSuccess Action<T>

The side effect invoked with the carried value.

Returns

Task<Result<T>>

A task that completes with the awaited result, unchanged, so calls can be chained.

Type Parameters

T

The 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

source or onSuccess is 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

source Task<Result<T>>

The task producing the result to observe. Must not be null.

onSuccess Func<T, Task>

The asynchronous side effect invoked with the carried value.

Returns

Task<Result<T>>

A task that completes with the awaited result, unchanged, so calls can be chained.

Type Parameters

T

The 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

source or onSuccess is 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

source Result<T>

The result to observe.

onFailure Func<ResultError, Task>

The asynchronous side effect invoked with the carried error.

Returns

Task<Result<T>>

A task that completes with this result, unchanged, so calls can be chained.

Type Parameters

T

The 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

onFailure is 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

source Task<Result<T>>

The task producing the result to observe. Must not be null.

onFailure Action<ResultError>

The side effect invoked with the carried error.

Returns

Task<Result<T>>

A task that completes with the awaited result, unchanged, so calls can be chained.

Type Parameters

T

The 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

source or onFailure is 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

source Task<Result<T>>

The task producing the result to observe. Must not be null.

onFailure Func<ResultError, Task>

The asynchronous side effect invoked with the carried error.

Returns

Task<Result<T>>

A task that completes with the awaited result, unchanged, so calls can be chained.

Type Parameters

T

The 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

source or onFailure is null.

Applies to

ProductVersions
.NET8, 10