Concept

async/await is a modern Swift concurrency feature used to handle asynchronous operations in a synchronous-looking way.

In networking, it replaces completion handlers with a clean and linear flow.


Key Idea

Request
↓
await response
↓
process result
↓
handle errors

URLSession with Async/Await

Instead of using dataTask, async/await uses:

let (data, response) = try await URLSession.shared.data(for: request)

This performs the network request and waits for the result.


Async Function

Functions using async/await must be marked as:

func getUsers() async

Calling Async Functions

Async functions are called using:

Task {
    await getUsers()
}