It helps you write code that runs tasks at the same time without making the code hard to read or understand.
Click to reveal answer
beginner
How does async/await improve code readability compared to callbacks?
It makes asynchronous code look like normal, straight-line code, so it’s easier to follow and debug.
Click to reveal answer
beginner
What keyword do you use in Swift to mark a function that runs asynchronously?
You use the async keyword before the function’s return type.
Click to reveal answer
beginner
What does the await keyword do in Swift async code?
It pauses the current function until the asynchronous task finishes, then continues with the result.
Click to reveal answer
intermediate
Why is using async/await safer than using threads directly?
Because async/await manages waiting and switching tasks automatically, reducing bugs from manual thread handling.
Click to reveal answer
What does the async keyword indicate in Swift?
AThe function is private
BThe function runs synchronously
CThe function is deprecated
DThe function runs asynchronously and can be awaited
✗ Incorrect
The async keyword marks a function that runs asynchronously and can be paused with await.
What happens when you use await in Swift async code?
AThe function pauses until the awaited task finishes
BThe function runs in a new thread
CThe function immediately returns
DThe function crashes
✗ Incorrect
await pauses the current function until the asynchronous task completes, then resumes.
Why is async/await easier to read than nested callbacks?
ABecause it uses fewer lines of code
BBecause it runs faster
CBecause it looks like normal sequential code
DBecause it uses global variables
✗ Incorrect
Async/await makes asynchronous code look like normal sequential code, improving readability.
Which of these is a benefit of async/await in Swift?
AAutomatic task suspension and resumption
BIgnoring errors
CBlocking the main thread
DManual thread management
✗ Incorrect
Async/await automatically suspends and resumes tasks, simplifying concurrency.
What problem does async/await help avoid?
AWriting synchronous code
BCallback hell and complex nested code
CUsing too many variables
DSlow compilation
✗ Incorrect
Async/await helps avoid deeply nested callbacks, often called callback hell.
Explain how async/await changes the way you write concurrent code in Swift.
Think about how async/await makes asynchronous code look like normal code.
You got /4 concepts.
Describe the benefits of using async/await over traditional callback methods.
Consider how callbacks can get complicated and how async/await helps.
You got /4 concepts.
Practice
(1/5)
1. What is the main benefit of using async/await in Swift for concurrent tasks?
easy
A. It replaces all synchronous code with asynchronous code.
B. It automatically speeds up the app without any code changes.
C. It prevents any errors from happening in the code.
D. It makes asynchronous code easier to read and write.
Solution
Step 1: Understand async/await purpose
Async/await helps write asynchronous code that looks like normal, simple code.
Step 2: Compare benefits
It does not automatically speed up or replace all code, nor prevent all errors, but it improves readability and flow.
Final Answer:
It makes asynchronous code easier to read and write. -> Option D
Quick Check:
Async/await improves code readability = C [OK]
Hint: Async/await makes async code look like normal code [OK]
Common Mistakes:
Thinking async/await automatically speeds up code
Believing async/await removes all errors
Confusing async/await with synchronous code
2. Which of the following is the correct syntax to declare an asynchronous function in Swift?
easy
A. async func fetchData() throws -> String
B. func fetchData() async throws -> String
C. func async fetchData() throws -> String
D. func fetchData() throws async -> String
Solution
Step 1: Recall async function syntax
In Swift, the async keyword comes after the function parentheses.
Step 2: Check option syntax
func fetchData() async throws -> String correctly places async after parentheses and before throws.
Final Answer:
func fetchData() async throws -> String -> Option B
Quick Check:
Async keyword after parentheses = A [OK]
Hint: Remember async goes right after function parentheses [OK]
Common Mistakes:
Placing async before func keyword
Putting async after throws keyword
Mixing order of async and throws
3. What will be printed by this Swift code using async/await?
func fetchNumber() async -> Int {
return 5
}
Task {
let number = await fetchNumber()
print(number)
}
medium
A. 5
B. Optional(5)
C. Task
D. No output
Solution
Step 1: Understand async function return
The function fetchNumber() returns an Int value 5 asynchronously.
Step 2: Await and print result
The await keyword waits for the result, then print(number) outputs 5.
Final Answer:
5 -> Option A
Quick Check:
Await fetch returns 5 printed = D [OK]
Hint: Await unwraps async result before printing [OK]
Common Mistakes:
Expecting Optional(5) instead of 5
Thinking Task prints itself
Assuming no output without await
4. Identify the error in this Swift async/await code:
func loadData() async -> String {
return "Data"
}
let result = await loadData()
print(result)
medium
A. Cannot use await outside an async context
B. Missing return type in function
C. Function should not be async
D. Await keyword is misspelled
Solution
Step 1: Check await usage context
In Swift, await must be called inside an async function or async context.
Step 2: Analyze code structure
The code calls await loadData() at top-level, which is not allowed.
Final Answer:
Cannot use await outside an async context -> Option A
Quick Check:
Await requires async context = A [OK]
Hint: Await only works inside async functions [OK]
Common Mistakes:
Calling await in synchronous code
Ignoring async context requirement
Assuming await works anywhere
5. You want to fetch user info and user posts concurrently using async/await in Swift. Which code correctly runs both tasks at the same time and waits for both results?
hard
A. let user = await fetchUser()
let posts = await fetchPosts()
B. fetchUser()
fetchPosts()
print("Done")
C. async let user = fetchUser()
async let posts = fetchPosts()
let userInfo = await user
let userPosts = await posts
D. Task {
let user = fetchUser()
let posts = fetchPosts()
print(user, posts)
}
Solution
Step 1: Understand concurrent async calls
Using async let starts tasks concurrently without waiting immediately.
Step 2: Await both results
Awaiting user and posts later waits for both to finish, running in parallel.
Final Answer:
async let user = fetchUser()
async let posts = fetchPosts()
let userInfo = await user
let userPosts = await posts -> Option C
Quick Check:
async let runs tasks concurrently = B [OK]
Hint: Use async let to run tasks in parallel [OK]
Common Mistakes:
Awaiting tasks one after another (sequential)
Not awaiting async let variables
Calling async functions without await
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.