What does await do in Swift structured concurrency?

Generated on 8/21/2024

1 search

In Swift structured concurrency, the await keyword is used to indicate that a suspension can occur at that line in the code. When a task is waiting for an asynchronous operation to complete, such as reading from the disk or messaging another service, it suspends to yield the CPU to other tasks that have work to do. This allows for efficient use of resources by letting other tasks execute while waiting for the asynchronous operation to finish. The async keyword is used to mark functions that may suspend, and await is used when calling these functions to handle the potential suspension.

For more details, you can refer to the session A Swift Tour: Explore Swift’s features and design (19:10).