Component - Await keyword
This UI component demonstrates how the await keyword works in Swift concurrency. It shows a button that, when tapped, waits for a simulated network call to finish before updating the text on screen.
This UI component demonstrates how the await keyword works in Swift concurrency. It shows a button that, when tapped, waits for a simulated network call to finish before updating the text on screen.
VStack ├── Text └── Button
await keyword do in Swift's async functions?awaitawait keyword pauses the execution of an async function until the awaited task finishes.await does not perform.await pauses async tasks = A [OK]await inside it?func name() async {} to declare an async function.await inside async functionawait inside an async function. Options B, C, and D have syntax errors.func fetchNumber() async -> Int {
return 5
}
func printNumber() async {
let number = await fetchNumber()
print(number)
}
Task {
await printNumber()
}fetchNumber() returns 5 asynchronously. Using await waits for this result.printNumber() function awaits fetchNumber() and prints the returned value 5.func loadData() async -> String {
return "Data"
}
func showData() {
let result = await loadData()
print(result)
}showData() is not marked async but uses await, which is invalid.await can only be used inside async functions. So showData() must be async to use await.await?fetchUser() and fetchPosts(), ensuring sequential completion. func loadUserData() async {
let user = await fetchUser()
let posts = fetchPosts(user.id)
print(posts)
} misses await on fetchPosts().Pass the test and this pattern moves unknown