Logo for tanaschita.com

How to use async/await in synchronous Swift code with tasks

Learn how to use tasks to call async/await methods from synchronous code.

25 Aug 2025 · 3 min read

When working with async/await in Swift, we might need to call async/await methods or properties from synchronous contexts. Since those contexts don't allow the await keyword directly, we need a way to bridge the gap. That's where tasks come in.

Let's jump in and look at an example.

Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE

Let's say we have an async function that fetches some data:

func fetchData() async -> String {
...
}

Now, we want to call it when the user taps on a button inside a SwiftUI view.

struct ContentView: View {
var body: some View {
Button("Load Data") {
await fetchData()
}
}
}

With this in place, we get the compiler error async call in a function that does not support concurrency. A button's action is synchronous, so await doesn't work directly. To solve it, we can wrap the call inside a Task:

struct ContentView: View {
var body: some View {
Button("Load Data") {
Task {
await fetchData()
}
}
}
}

A Task starts running immediately after creation, and it lets us use await inside otherwise synchronous code. We don't need to hold on to it unless we want extra control (like cancellation).

This pattern is useful in many places where async code meets synchronous APIs. For example, in app lifecycle methods such as application(\_:didFinishLaunchingWithOptions:) in AppDelegate, we can use the same Task { ... } approach to call async methods like requesting push notification permissions.

Tasks give us a clean and lightweight way to bridge between Swift's structured concurrency and existing synchronous code.

Sponsorship logo
Preparing for a technical iOS job interview
Preparing for a technical iOS Job Interview with over 300 questions & answers. Covering Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, iOS File System, Core Data, Concurrency with async/await, Security, Automated Testing, Dependency Management, AI & Machine Learning and more.
LEARN MORE
Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE
Sponsorship logo
Become a sponsor of tanaschita.com
By publishing an article on different iOS topics every week, tanaschita.com is constantly growing in the developer community and may provide a great audience for you as a sponsor.
CLICK TO LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

async/await

concurrency

swift

Understanding Sendable in Swift

Learn what Sendable means in Swift concurrency and how it helps prevent data races.

06 Jul 2026 · 8 min read

Latest articles and tips

© 2026 tanaschita.com

Privacy policy

Impressum