Computation Expression

AsyncSeq Computation Expression Extensions

Namespace: FsToolkit.ErrorHandling

Package: FsToolkit.ErrorHandling.AsyncSeq

This module extends the asyncResult computation expression with support for iterating over AsyncSeqarrow-up-right sequences directly inside asyncResult { } blocks.

Without this extension, consuming an AsyncSeq inside asyncResult would require manual collection or conversion. With it, you can use for loop syntax naturally.

Extended Members

The following members are added to AsyncResultBuilder:

  • For(xs: AsyncSeq<Result<'T, 'TError>>, binder) — iterates over an AsyncSeq of Result values; short-circuits on the first Error.

  • For(xs: AsyncSeq<'T>, binder) — iterates over an AsyncSeq of plain values inside asyncResult; each item is treated as Ok.

  • While(guard, computation) — supports while loops driven by an AsyncSeq enumerator.

Examples

Example 1

Processing each item from an AsyncSeq of plain values inside asyncResult:

open FsToolkit.ErrorHandling
open FSharp.Control

let processItems (items: AsyncSeq<Item>) : Async<Result<unit, string>> =
    asyncResult {
        for item in items do
            do! processItem item  // processItem : Item -> Async<Result<unit, string>>
    }
// Iterates until all items are processed, or stops on the first Error

Example 2

Iterating over an AsyncSeq<Result<'T, 'TError>>, short-circuiting on the first error:

Example 3

Reading lines from a stream as an AsyncSeq and writing them to a database:

Last updated