|
| 1 | +# CancellableValueTaskOption.bind |
| 2 | + |
| 3 | +Namespace: `FsToolkit.ErrorHandling` |
| 4 | + |
| 5 | +## Function Signature |
| 6 | + |
| 7 | +```fsharp |
| 8 | +('input -> CancellableValueTask<'output option>) -> CancellableValueTask<'input option> -> CancellableValueTask<'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 -> CancellableValueTask<Account option> |
| 21 | +let lookupAccountByEmail email = cancellableValueTask { |
| 22 | + let john = { EmailAddress = "john@test.com"; Name = "John Johnson" } |
| 23 | + let jeff = { EmailAddress = "jeff@test.com"; Name = "Jeff Jefferson" } |
| 24 | + let jack = { EmailAddress = "jack@test.com"; Name = "Jack Jackson" } |
| 25 | + |
| 26 | + let accounts = Map.ofList [ |
| 27 | + ("john@test.com", john) |
| 28 | + ("jeff@test.com", jeff) |
| 29 | + ("jack@test.com", jack) |
| 30 | + ] |
| 31 | + |
| 32 | + return Map.tryFind email accounts |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### Example 1 |
| 37 | + |
| 38 | +```fsharp |
| 39 | +let taskOpt : CancellableValueTask<Account option> = |
| 40 | + CancellableValueTaskOption.some "john@test.com" // CancellableValueTask<string option> |
| 41 | + |> CancellableValueTaskOption.bind lookupAccountByEmail // CancellableValueTask<Account option> |
| 42 | +
|
| 43 | +// cancellableValueTask { Some { EmailAddress = "john@test.com"; Name = "John Johnson" } } |
| 44 | +``` |
| 45 | + |
| 46 | +### Example 2 |
| 47 | + |
| 48 | +```fsharp |
| 49 | +let taskOpt : CancellableValueTask<Account option> = |
| 50 | + CancellableValueTaskOption.some "jerry@test.com" // CancellableValueTask<string option> |
| 51 | + |> CancellableValueTaskOption.bind lookupAccountByEmail // CancellableValueTask<Account option> |
| 52 | +
|
| 53 | +// cancellableValueTask { None } |
| 54 | +``` |
| 55 | + |
| 56 | +### Example 3 |
| 57 | + |
| 58 | +```fsharp |
| 59 | +let taskOpt : CancellableValueTask<Account option> = |
| 60 | + CancellableValueTask.singleton None // CancellableValueTask<string option> |
| 61 | + |> CancellableValueTaskOption.bind lookupAccountByEmail // CancellableValueTask<Account option> |
| 62 | +
|
| 63 | +// cancellableValueTask { None } |
| 64 | +``` |
0 commit comments