0Pricing
Swift Academy · Lesson

async/await Syntax and Calling Async Functions

Writing and calling async functions, understanding suspension points.

async/await Syntax and Calling Async Functions is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “async/await Syntax and Calling Async Functions” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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