|
| 1 | +# CancellableTaskOption.bind |
| 2 | + |
| 3 | +Namespace: `FsToolkit.ErrorHandling` |
| 4 | + |
| 5 | +## Function Signature |
| 6 | + |
| 7 | +```fsharp |
| 8 | +('input -> CancellableTask<'output option>) -> CancellableTask<'input option> -> CancellableTask<'output option> |
| 9 | +``` |
| 10 | + |
| 11 | +## Examples |
| 12 | + |
| 13 | +Take the following function for example |
| 14 | + |
| 15 | +```fsharp |
| 16 | +type Account = |
| 17 | + { EmailAddress : string |
| 18 | + Name : string } |
| 19 | +
|
| 20 | +// string -> CancellableTask<Account option> |
| 21 | +let lookupAccountByEmail email = cancellableTask { |
| 22 | + let john = { EmailAddress = "[email protected]"; Name = "John Johnson" } |
| 23 | + let jeff = { EmailAddress = "[email protected]"; Name = "Jeff Jefferson" } |
| 24 | + let jack = { EmailAddress = "[email protected]"; Name = "Jack Jackson" } |
| 25 | + |
| 26 | + // Just a map lookup, but imagine we look up an account in our database |
| 27 | + let accounts = Map.ofList [ |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + ] |
| 32 | + |
| 33 | + return Map.tryFind email accounts |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### Example 1 |
| 38 | + |
| 39 | +```fsharp |
| 40 | +let taskOpt : CancellableTask<Account option> = |
| 41 | + CancellableTaskOption.some "[email protected]" // CancellableTask<string option> |
| 42 | + |> CancellableTaskOption.bind lookupAccountByEmail // CancellableTask<Account option> |
| 43 | +
|
| 44 | +// cancellableTask { Some { EmailAddress = "[email protected]"; Name = "John Johnson" } } |
| 45 | +``` |
| 46 | + |
| 47 | +### Example 2 |
| 48 | + |
| 49 | +```fsharp |
| 50 | +let taskOpt : CancellableTask<Account option> = |
| 51 | + CancellableTaskOption.some "[email protected]" // CancellableTask<string option> |
| 52 | + |> CancellableTaskOption.bind lookupAccountByEmail // CancellableTask<Account option> |
| 53 | +
|
| 54 | +// cancellableTask { None } |
| 55 | +``` |
| 56 | + |
| 57 | +### Example 3 |
| 58 | + |
| 59 | +```fsharp |
| 60 | +let taskOpt : CancellableTask<Account option> = |
| 61 | + CancellableTask.singleton None // CancellableTask<string option> |
| 62 | + |> CancellableTaskOption.bind lookupAccountByEmail // CancellableTask<Account option> |
| 63 | +
|
| 64 | +// cancellableTask { None } |
| 65 | +``` |
0 commit comments