Skip to content

Commit 67553db

Browse files
committed
Show syntax for tasks & task groups.
1 parent 6c9f651 commit 67553db

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

TSPL.docc/GuidedTour/GuidedTour.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,50 @@ let bakedGoods = await [cookies, bread]
23162316
```
23172317
-->
23182318

2319+
You structure concurrent code using task groups and child tasks.
2320+
Writing `async`-`let` implicitly creates a child task.
2321+
2322+
```swift
2323+
let desserts = await withTaskGroup(of: String.self) { group in
2324+
for recipe in ["biscuit", "croissant", "egg tart"] {
2325+
group.addTask {
2326+
return await bake(recipe)
2327+
}
2328+
}
2329+
2330+
var results: [String] = []
2331+
for await result in group {
2332+
results.append(result)
2333+
}
2334+
2335+
return results
2336+
}
2337+
```
2338+
2339+
2340+
<!--
2341+
- test: guided-tour-async
2342+
2343+
``` swifttest
2344+
-> let desserts = await withTaskGroup(of: String.self) { group in
2345+
for recipe in ["biscuit", "croissant", "egg tart"] {
2346+
group.addTask {
2347+
return await bake(recipe)
2348+
}
2349+
}
2350+
---
2351+
var results: [String] = []
2352+
for await result in group {
2353+
results.append(result)
2354+
}
2355+
---
2356+
return results
2357+
}
2358+
>> print(desserts)
2359+
<< ["biscuit", "croissant", "egg tart"]
2360+
```
2361+
-->
2362+
23192363
Actors are similar to classes,
23202364
except they ensure that different asynchronous functions
23212365
can all interact with an instance of the same actor at the same time.

0 commit comments

Comments
 (0)