Skip to content

Latest commit

 

History

History
65 lines (47 loc) · 1.51 KB

File metadata and controls

65 lines (47 loc) · 1.51 KB

AsyncOption.bind

Namespace: FsToolkit.ErrorHandling

Function Signature

('TInput -> Async<'TOutput option>) -> Async<'TInput option> -> Async<'TOutput option>

Examples

Take the following function for example

type Account =
  { EmailAddress : string
    Name : string }

// string -> Async<Account option>
let lookupAccountByEmail email = async {
  let john = { EmailAddress = "[email protected]"; Name = "John Johnson" }
  let jeff = { EmailAddress = "[email protected]"; Name = "Jeff Jefferson" }
  let jack = { EmailAddress = "[email protected]"; Name = "Jack Jackson" }
  
  // Just a map lookup, but imagine we look up an account in our database
  let accounts = Map.ofList [
      ("[email protected]", john)
      ("[email protected]", jeff)
      ("[email protected]", jack)
  ]
  
  return Map.tryFind email accounts
}

Example 1

let asyncOpt : Async<Account option> =
    AsyncOption.some "[email protected]" // Async<string option>
    |> AsyncOption.bind lookupAccountByEmail // Async<Account option>

// async { Some { EmailAddress = "[email protected]"; Name = "John Johnson" } }

Example 2

let asyncOpt : Async<Account option> =
    AsyncOption.some "[email protected]" // Async<string option>
    |> AsyncOption.bind lookupAccountByEmail // Async<Account option>

// async { None }

Example 3

let asyncOpt : Async<Account option> =
    Async.singleton None // Async<string option>
    |> AsyncOption.bind lookupAccountByEmail // Async<Account option>

// async { None }