Complete the code to declare an async function named fetchData.
func fetchData() [1] {
// function body
}The async keyword declares that the function runs asynchronously.
Complete the code to call an async function fetchData and wait for its result.
Task {
let data = try? await [1]()
print(data ?? "No data")
}You must call the async function by its exact name and use await to wait for its result.
Fix the error in the async function declaration by filling the blank.
func loadData() [1] throws {
// function body
}The async keyword must be used to mark the function as asynchronous. It can be combined with throws but must be separate.
Fill both blanks to declare and call an async function correctly.
func fetchUser() [1] { // fetch user data } Task { let user = try? await [2]() print(user ?? "No user") }
The function must be declared with async. When calling it inside a Task, use await before the function name. Here, the function name is fetchUser.
Fill all three blanks to create an async function that throws and call it correctly.
func fetchData() [1] [2] { // fetch data } Task { do { let data = try [3] fetchData() print(data) } catch { print("Error") } }
The function must be declared with async and throws. When calling it inside a Task, use try and await to handle errors and wait for the result.