@@ -2257,6 +2257,124 @@ print(fridgeIsOpen)
22572257 ```
22582258-->
22592259
2260+ ## Concurrency
2261+
2262+ You use ` async ` to mark an asynchronous function.
2263+ You mark an asynchronous operation,
2264+ like a call to an asynchronous function,
2265+ by writing ` await `
2266+ if you want to wait for it to complete.
2267+
2268+ ``` swift
2269+ func bake (_ food : String ) async -> String {
2270+ // ... wait for food to bake ...
2271+ return food
2272+ }
2273+ func makeCookies () async -> String {
2274+ let cookies = await bake (" cookies" )
2275+ return cookies
2276+ }
2277+ ```
2278+
2279+ <!--
2280+ - test: `guided-tour`
2281+
2282+ ```swifttest
2283+ -> func bake(_ food: String) async -> String {
2284+ // ... wait for food to bake ...
2285+ return food
2286+ }
2287+ -> func makeCookies() async -> String {
2288+ let cookies = await bake("cookies")
2289+ return cookies
2290+ }
2291+ ```
2292+ -->
2293+
2294+ You can use ` async ` -` let ` to start an asynchronous operation
2295+ without waiting for it to complete,
2296+ which lets the operations run at the same time.
2297+ Because you still need to wait for the operation to finish
2298+ before you can use the value it returns,
2299+ you write ` await ` when you use that constant.
2300+
2301+ ``` swift
2302+ async let cookies = makeCookies ()
2303+ async let bread = bake (" bread" )
2304+
2305+ let bakedGoods = await [cookies, bread]
2306+ ```
2307+
2308+ <!--
2309+ - test: guided-tour-async
2310+
2311+ ```swifttest
2312+ -> async let cookies = makeCookies()
2313+ -> async let bread = bake("bread")
2314+ ---
2315+ -> let bakedGoods = await [cookies, bread]
2316+ ```
2317+ -->
2318+
2319+ Actors are similar to classes,
2320+ except they ensure that different asynchronous functions
2321+ can all interact with an instance of the same actor at the same time.
2322+
2323+ ``` swift
2324+ actor Oven {
2325+ var contents: [String ] = []
2326+ func bake (_ food : String ) -> String {
2327+ let index = contents.endIndex
2328+ contents.append (food)
2329+ // ... wait for food to bake ...
2330+ contents.remove (at : index)
2331+ return food
2332+ }
2333+ }
2334+ ```
2335+
2336+ <!--
2337+ - test: `guided-tour`
2338+
2339+ ```swifttest
2340+ -> actor Oven {
2341+ var contents: [String] = []
2342+ func bake(_ food: String) -> String {
2343+ let index = contents.endIndex
2344+ contents.append(food)
2345+ // ... wait for food to bake ...
2346+ contents.remove(at: index)
2347+ return food
2348+ }
2349+ }
2350+ ```
2351+ -->
2352+
2353+ When you call a method on an actor or access one of its properties,
2354+ you mark that code with ` await `
2355+ to indicate that it might have to wait for other code
2356+ that's already running on the actor to finish.
2357+
2358+ ``` swift
2359+ let oven = Oven ()
2360+ let biscuits = await oven.bake (" biscuits" )
2361+ for item in await oven.contents {
2362+ print (item)
2363+ }
2364+ ```
2365+
2366+ <!--
2367+ - test: `guided-tour`
2368+
2369+ ```swifttest
2370+ -> let oven = Oven()
2371+ -> let biscuits = await oven.bake("biscuits")
2372+ -> for item in await oven.contents {
2373+ print(item)
2374+ }
2375+ ```
2376+ -->
2377+
22602378## Generics
22612379
22622380Write a name inside angle brackets
0 commit comments