bind

Namespace: FsToolkit.ErrorHandling

bind applies an async function to the value inside an AsyncValidation if it is Ok, returning the resulting AsyncValidation. Like Validation.bind, this uses monadic (short-circuit) semantics — if the input is an Error, the binder function is not called and the error is returned immediately.

Function Signature

('okInput -> AsyncValidation<'okOutput, 'error>) -> AsyncValidation<'okInput, 'error> -> AsyncValidation<'okOutput, 'error>

Examples

Take the following function for example:

// string -> AsyncValidation<int, string>
let tryParseIntAsync (s: string) =
    async {
        match System.Int32.TryParse(s) with
        | true, i -> return Ok i
        | false, _ -> return Error [ $"'%s{s}' is not a valid integer" ]
    }

Example 1

let result =
    AsyncValidation.ok "42"
    |> AsyncValidation.bind tryParseIntAsync

// async { Ok 42 }

Example 2

Example 3

Last updated