Async and await: suspension is not blocking
Async and await: suspension is not blocking
A report may need data from an operation that finishes later. An async function can suspend while waiting; await marks a possible suspension point. This allows other work to progress, but it does not promise a new thread, a particular scheduling order or parallel execution.
What you’ll learn
- Trace an awaited result before dependent output.
- Explain why async is not a dedicated-thread guarantee.
Async and await: suspension is not blocking in the example
Call starts
A report may need data from an operation that finishes later.
Call startsAwait result
An async call returns its result when the awaited operation completes.
Await resultDependent display
The caller prints Before request, awaits the function’s result, then prints the score and final message.
Dependent display
How async and await: suspension is not blocking works
An async call returns its result when the awaited operation completes. The example uses an immediate async function to isolate the syntax, not a network request. Real fetching also needs errors, cancellation and appropriate isolation for shared state.
Work through async and await: suspension is not blocking
Save this complete program as main.swift and run it with the Swift toolchain. Use a toolchain supporting the demonstrated async top-level code.
Swift · complete program
func loadScore() async -> Int { 80 }
print("Before request")
let score = await loadScore()
print("Score:", score)
print("After result")Expected output
Before request
Score: 80
After resultFollow the execution
The caller prints Before request, awaits the function’s result, then prints the score and final message. The await establishes the dependency before score is used. There is no timed delay or network activity in this teaching function.
Try this program in the Swift compiler. Paste the complete source above.
Practice: Async and await: suspension is not blocking
Your task: Does adding async guarantee loadScore executes on a separate thread?
Swift · complete program
func loadScore() async -> Int { 80 }
print("Before request")
let score = await loadScore()
print("Score:", score)
print("After result")Worked answer
No. Async describes the ability to suspend under Swift’s concurrency model, not a promise of a dedicated thread. Isolation and execution rules depend on the declaration, language mode and context.
Do not infer a thread from async
Use throws with async when an operation can fail. A suspension point can allow other operations to run; never assume shared mutable state stays unchanged across an await simply because nearby source lines look sequential.
Check your decision
Do not infer a thread from async
No. Async describes the ability to suspend under Swift’s concurrency model, not a promise of a dedicated thread. Isolation and execution rules depend on the declaration, language mode and context. Use throws with async when an operation can fail.
Further reading: official Swift language guide.
Reaching the recap and completing the quizzes are separate milestones.