async await
Asked on 2024-07-27
17 searches
The async and await keywords in Swift are used to handle asynchronous operations, allowing functions to suspend and resume without blocking the main thread. Here are some key points about async and await from the WWDC sessions:
-
Basic Usage:
- A function that may suspend is marked with the
asynckeyword. - When an
asyncfunction is called, theawaitkeyword is used to indicate that a suspension can occur on that line. - This allows the CPU to be yielded to other tasks while waiting for an asynchronous operation to complete.
- Example:
func fetchData() async -> Data { let data = await fetchFromNetwork() return data }
- A function that may suspend is marked with the
-
Concurrency in Swift:
- Swift's concurrency model includes tasks,
async/await, and actors. - Actors are reference types that encapsulate shared mutable state and automatically protect their state by serializing accesses.
- Calls to actor methods from outside the context of the actor are asynchronous and require the
awaitkeyword. - Example:
actor UserStore { var users: [String] = [] func addUser(_ user: String) { users.append(user) } } let userStore = UserStore() await userStore.addUser("Alice")
- Swift's concurrency model includes tasks,
-
Performance Considerations:
- Async functions are split into multiple partial functions at runtime to handle suspension points.
- They keep their local state on a separate stack from the C stack.
- This design helps in managing memory efficiently and ensures that async tasks do not block precious C threads.
- Example:
func processTasks() async { for task in tasks { let result = await task.perform() results.append(result) } }
-
Testing Asynchronous Code:
- When writing concurrent test code, you can use the same concurrency features in Swift as in production code.
awaitworks the same way in tests, allowing other test code to keep the CPU busy while work is pending.- Swift provides async overloads for most completion handlers, making it easier to test asynchronous code.
- Example:
func testAsyncFunction() async { let result = await someAsyncFunction() XCTAssertEqual(result, expectedValue) }
For more detailed information, you can refer to the following sessions from WWDC 2024:
- A Swift Tour: Explore Swift’s features and design
- Explore Swift performance
- Go further with Swift Testing
These sessions cover the basics of async/await, concurrency in Swift, performance considerations, and testing asynchronous code.

A Swift Tour: Explore Swift’s features and design
Learn the essential features and design philosophy of the Swift programming language. We’ll explore how to model data, handle errors, use protocols, write concurrent code, and more while building up a Swift package that has a library, an HTTP server, and a command line client. Whether you’re just beginning your Swift journey or have been with us from the start, this talk will help you get the most out of the language.

Go further with Swift Testing
Learn how to write a sweet set of (test) suites using Swift Testing’s baked-in features. Discover how to take the building blocks further and use them to help expand tests to cover more scenarios, organize your tests across different suites, and optimize your tests to run in parallel.

Migrate your app to Swift 6
Experience Swift 6 migration in action as we update an existing sample app. Learn how to migrate incrementally, module by module, and how the compiler helps you identify code that’s at risk of data races. Discover different techniques for ensuring clear isolation boundaries and eliminating concurrent access to shared mutable state.
