traverseAsyncResult

Option.traverseAsyncResult

Namespace: FsToolkit.ErrorHandling

Function Signature:

('a -> Async<Result<'b,'c>>) -> 'a option -> Async<Result<'b option, 'c>>

Note that traverse is the same as map >> sequence. See also Option.sequenceAsyncResult.

See also Scott Wlaschin's Understanding traverse and sequencearrow-up-right.

Examples

Example 1

Say we have a function to get a number from a database (asynchronously), and multiply our input by that number if it's found:

let tryMultiplyWithDatabaseValue: float -> Async<Result<float, string>> = // ...

If we start with an optional vaue, then we could map this funciton using Option.traverseAsyncResult as follows:

let input = Some 1.234

input // float option
|> Option.traverseAsyncResult tryMultiplyWithDatabaseValue // Async<Result<float option, string>>

If we combine this with the AsyncResult computation expression, we could directly let! the output:

asyncResult {
    let input = Some 1.234

    let! output = // float option
        input // float option
        |> Option.traverseAsyncResult tryMultiplyWithDatabaseValue // Async<Result<float option, string>>
}

Last updated