Requirements: Link to heading
To understand this tutorial we need knowledge about:
- Combine: Swift 5, iOS13, WWDC 2019
- Async/Await – Swift 5.5, iOS 15, WWDC 2021
Use case where async/await replaces Combine: Link to heading
The use case is called asynchronous in the network layer in a project. One way to implement that is with Combine, but in this use case we implement it with async/await.
To make the call asynchronous, the fetchTodos method must be marked as asynchronous with async. Then, within this method, you can make asynchronous calls using try await and decode the data using JSONDecoder().decode(). If any errors occur during these operations, they will be propagated as an exception that you can handle in the method caller.

We use Task to call fetchTodos() asynchronously, and ‘try await’ to get the results of the fetchTodos() call and handle any errors that may occur. Next, we update the UI on the main thread using DispatchQueue.main.async.

Notes: In Swift, Task is a new type introduced in Swift 5.5 as part of the structured concurrency features. A task represents a unit of asynchronous work that can be executed concurrently in a thread. It is a way to execute asynchronous and concurrent code in a more controlled and structured way in Swift and can be used in combination with the new async and await keywords to write asynchronous code in a cleaner and more readable way.
-
async/await: This provides a more direct and less abstract way of working with asynchronous code. You can write asynchronous code in a similar way to traditional synchronous code, which can make it easier for many developers to understand.
-
async/await: It is specifically designed to handle asynchronous tasks, such as network calls, I/O operations, and other operations that may block the main thread. It is especially useful for performing asynchronous operations in a synchronous and more readable manner.
-
async/await: It was introduced in Swift 5.5 and is available in iOS 15, macOS 12 and later.
-
Combine: Introduced in iOS 13 and macOS 10.15.
Conclusion: Link to heading
In summary, although async/await and Combine can achieve similar goals in terms of handling asynchronous tasks, they have different abstraction levels and approaches. Choosing between them depends on the specific needs of your app, your familiarity with the different approaches and the versions of iOS/macOS you’re using.