Async¶
Every operator also works on Task<Result<T>>, Task<Result> and Task<Option<T>>, so a pipeline stays one expression after the first asynchronous call. This section covers those overloads, running tasks in parallel, retrying, and streaming.
In this section¶
- Task pipelines: the
Taskoverloads, the...Awaitvariants for asynchronous lambdas, async query syntax,Out,Matchand cancellation. - Concurrency and resilience:
Result.WhenAll, collecting all errors,Retry,RetryAsyncandTimeout. - Streams:
IAsyncEnumerable<Result<T>>andIAsyncEnumerable<Option<T>>.
Quick example¶
using Fluent.Unions;
Result<string> label = await LoadOrderAsync(id)
.Ensure(order => order.Items.Count > 0, "The order has no items.")
.ChainAwait(order => LoadCustomerAsync(order.CustomerId))
.Select(customer => customer.Name)
.Catch((KeyNotFoundException _) => "unknown customer");
Without the overloads, each step would need its own await and its own state check.
When to use¶
- You call asynchronous methods that return
Task<Result<T>>orTask<Option<T>>and want to chain them without intermediate variables. - You start a pipeline from code that throws, with
Result.TryAsync. - You run several results in parallel, retry a flaky call, or consume a stream of results.
Rules¶
- A faulted task becomes an error result. For options, it stays faulted, because an option has no error state.
awaitthrows the original exception. - A lambda that throws inside a result operator becomes the error. Inside an option operator, it propagates.
- The operators take no
CancellationToken, because they do no I/O. Capture the token in your lambdas. - Use the
...Awaitvariants when the lambda returns aTask. The plain operators take synchronous lambdas. - A result type declared with
[FluentResult<TError>]has everyTaskoverload and...Awaitoperator, likeResult<T>.
Back to index: Docs index ยท Next: Task pipelines