File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff 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+
23192363Actors are similar to classes,
23202364except they ensure that different asynchronous functions
23212365can all interact with an instance of the same actor at the same time.
You can’t perform that action at this time.
0 commit comments