Structured concurrency, async functions
Define async functions, call them with await , and understand structured concurrency : child tasks finish before the parent scope exits.
Structured concurrency, async functions is a free Swift Academy lesson on CoddyKit — lesson 1 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why async/await?
async/await lets you write asynchronous code that reads like sync code. With structured concurrency, child tasks are tied to scope lifetimes—no leaks.
Your first async function
An async function can await other async work. Create a Task in scripts/playgrounds to call it.
// Simulate a short async delay, then return a value
func fetchNumber() async -> Int {
try? await Task.sleep(nanoseconds: 200_000_000) // ~200ms
return 42
}
// Call sites must use "await" from an async context or a Task
Task {
let n = await fetchNumber()
print("Fetched:", n) // 42
}Suspension, then resume
await suspends the task until the result is ready, then execution resumes cleanly at the next line.
func greet(after seconds: Double) async -> String {
let ns = UInt64(seconds * 1_000_000_000)
try? await Task.sleep(nanoseconds: ns)
return "Hello after \(Int(seconds))s"
}
Task {
print(await greet(after: 1.0))
print("Back on the same task after await.")
}async let basics
Use async let to start child tasks. They belong to the current scope and must be awaited before scope exits.
// Run child tasks concurrently within the same scope
func fetchUser() async -> String { "Ana" }
func fetchScore() async -> Int { 90 }
Task {
async let name = fetchUser()
async let score = fetchScore()
// Both start immediately; await joins them before use
let summary = await "\(name): \(score)"
print(summary) // "Ana: 90"
// Scope ends -> child tasks are awaited automatically (structured)
}Parents join children
Structured means parents cannot finish before children. This prevents orphaned work and makes lifetimes predictable.
// Parent Task encapsulates children lifetimes
func loadProfile() async -> String {
async let name = fetchUser()
async let score = fetchScore()
return await "\(name) (\(score))"
}
Task {
let profile = await loadProfile()
print(profile) // e.g., "Ana (90)"
// loadProfile does not return until its children are done
}Guidelines
Tips:
- Think in tasks, not threads.
- Use async let for sibling work;
awaitbefore scope exits. - Sleep with
Task.sleepfor demos; real apps await network/file I/O.
Definition of await
Quick check: What does await do?
Recap
Recap: Mark functions async, call with await. Use async let to start sibling tasks and rely on structured concurrency to end children before parents.
Frequently asked questions
Is the “Structured concurrency, async functions” lesson free?
Yes — the full text of “Structured concurrency, async functions” is free to read here on the web, and the Swift Academy course includes 3 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 “Structured concurrency, async functions”?
Define async functions, call them with await , and understand structured concurrency : child tasks finish before the parent scope exits. 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 3, so you can start here or from the beginning and move at your own pace.
How long does the “Structured concurrency, 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
- Structured concurrency, async functions
- async let, Task, cancellation
- try await and error propagation