Skip to content

Latest commit

 

History

History
90 lines (60 loc) · 1.79 KB

File metadata and controls

90 lines (60 loc) · 1.79 KB

AsyncValidation.orElse / AsyncValidation.orElseWith

Namespace: FsToolkit.ErrorHandling

orElse

orElse returns the original AsyncValidation if it is Ok, otherwise returns the provided alternative AsyncValidation.

Function Signature

AsyncValidation<'ok, 'errorOutput> -> AsyncValidation<'ok, 'errorInput> -> AsyncValidation<'ok, 'errorOutput>

Examples

Example 1

let result =
    AsyncValidation.ok "First"
    |> AsyncValidation.orElse (AsyncValidation.ok "Second")

// async { Ok "First" }

Example 2

let result =
    AsyncValidation.error "First"
    |> AsyncValidation.orElse (AsyncValidation.ok "Second")

// async { Ok "Second" }

Example 3

let result =
    AsyncValidation.error "First"
    |> AsyncValidation.orElse (AsyncValidation.error "Second")

// async { Error ["Second"] }

orElseWith

orElseWith returns the original AsyncValidation if it is Ok, otherwise calls the given function with the error list and returns its result.

Function Signature

('errorInput list -> AsyncValidation<'ok, 'errorOutput>) -> AsyncValidation<'ok, 'errorInput> -> AsyncValidation<'ok, 'errorOutput>

Examples

Example 1

let result =
    AsyncValidation.ok "First"
    |> AsyncValidation.orElseWith (fun _ -> AsyncValidation.ok "Second")

// async { Ok "First" }

Example 2

let result =
    AsyncValidation.error "First"
    |> AsyncValidation.orElseWith (fun _ -> AsyncValidation.ok "Second")

// async { Ok "Second" }

Example 3

let result =
    AsyncValidation.error "First"
    |> AsyncValidation.orElseWith (fun errors ->
        AsyncValidation.error $"Recovered from: %A{errors}")

// async { Error ["Recovered from: [\"First\"]"] }