Skip to content

Commit 108eae0

Browse files
LanayxBillWagner
andauthored
Update task-expressions.md (#45850)
* Update task-expressions.md Added `and!` section * Update docs/fsharp/language-reference/task-expressions.md --------- Co-authored-by: Bill Wagner <[email protected]>
1 parent a214493 commit 108eae0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/fsharp/language-reference/task-expressions.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ let makeTask() =
6868
makeTask() |> ValueTask<int>
6969
```
7070

71+
## `and!` bindings (starting from F# 10)
72+
73+
Within task expressions, it is possible to concurrently await for multiple asynchronous operations (`Task<'T>`, `ValueTask<'T>`, `Async<'T>` etc). Compare:
74+
75+
```fsharp
76+
// We'll wait for x to resolve and then for y to resolve. Overall execution time is sum of two execution times.
77+
let getResultsSequentially() =
78+
task {
79+
let! x = getX()
80+
let! y = getY()
81+
return x, y
82+
}
83+
84+
// x and y will be awaited concurrently. Overall execution time is the time of the slowest operation.
85+
let getResultsConcurrently() =
86+
task {
87+
let! x = getX()
88+
and! y = getY()
89+
return x, y
90+
}
91+
```
92+
7193
## Adding cancellation tokens and cancellation checks
7294

7395
Unlike F# async expressions, task expressions do not implicitly pass a cancellation token and don't implicitly perform cancellation checks. If your code requires a cancellation token, you should specify the cancellation token as a parameter. For example:

0 commit comments

Comments
 (0)