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.
('okInput -> AsyncValidation<'okOutput, 'error>) -> AsyncValidation<'okInput, 'error> -> AsyncValidation<'okOutput, 'error>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" ]
}let result =
AsyncValidation.ok "42"
|> AsyncValidation.bind tryParseIntAsync
// async { Ok 42 }let result =
AsyncValidation.ok "not a number"
|> AsyncValidation.bind tryParseIntAsync
// async { Error ["'not a number' is not a valid integer"] }let result =
AsyncValidation.error "previous error"
|> AsyncValidation.bind tryParseIntAsync
// async { Error ["previous error"] }