Swift Academy · Lesson

async/await Syntax and Calling Async Functions

Writing and calling async functions, understanding suspension points.

Lesson 1 of 412 steps

async/await Syntax and Calling Async Functions is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Welcome

Swift's async/await syntax makes asynchronous code read like synchronous code. `async` marks a function that can suspend; `await` marks a suspension point.

async Function

```swift func fetchUser(id: Int) async throws -> User { let url = URL(string: "https://api.example.com/users/\(id)")! let (data, _) = try await URLSession.shared.data(from: url) return try JSONDecoder().decode(User.self, from: data) } ```

Calling with await

```swift Task { let user = try await fetchUser(id: 42) print(user.name) } ```

Suspension Points

`await` is a suspension point — the function pauses here and returns the thread to the system. When the async operation completes, execution resumes.

async on MainActor

```swift @MainActor func updateUI() async { let data = try? await fetchData() label.text = data?.description ?? "Error" } ```

Bridging Callbacks to async

```swift func fetchAsync() async throws -> Data { try await withCheckedThrowingContinuation { cont in URLSession.shared.dataTask(with: url) { data, _, error in if let data { cont.resume(returning: data) } else { cont.resume(throwing: error!) } }.resume() } } ```

Error Handling with try await

```swift do { let user = try await fetchUser(id: 1) render(user) } catch URLError.notConnectedToInternet { showOfflineMessage() } catch { showError(error) } ```

await in for Loops

```swift for id in userIds { let user = try await fetchUser(id: id) // sequential process(user) } ``` This fetches one at a time. For concurrent fetching, use async let.

Returning Values

```swift func sum(a: Int, b: Int) async -> Int { await Task.sleep(nanoseconds: 1_000_000) return a + b } let result = await sum(a: 3, b: 4) // 7 ```

Async Properties

```swift var thumbnail: UIImage { get async { try! await loadImage(url: imageURL) } } let img = await post.thumbnail ```

Quick Check

What does `await` mean at a suspension point?

Recap

Key takeaways: • `async` — marks a suspendable function • `await` — suspension point; thread is freed while waiting • `try await` for async throwing functions • `@MainActor` for UI work • `withCheckedThrowingContinuation` bridges callbacks Next: Task and cancellation.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “async/await Syntax and Calling Async Functions” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “async/await Syntax and Calling Async Functions”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “async/await Syntax and Calling Async Functions”?

Writing and calling async functions, understanding suspension points. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “async/await Syntax and Calling Async Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. async/await Syntax and Calling Async Functions
  2. Task and Task Cancellation
  3. async let and Concurrent Child Tasks
  4. Actors: Protecting Shared Mutable State
← Back to Swift Academy