Complete the code to wait for the asynchronous function to finish before printing the result.
func fetchData() async -> String {
return "Data loaded"
}
func load() async {
let result = [1] fetchData()
print(result)
}The await keyword pauses the function until the asynchronous call completes, then returns the result.
Complete the code to mark the function as asynchronous so it can use 'await'.
func fetchUser() [1] -> String { return "User data" }
The async keyword marks a function as asynchronous, allowing it to use await inside.
Fix the error in calling an async function inside an asynchronous function by adding the missing keyword.
func syncFunction() async {
let data = [1] fetchData()
print(data)
}You must use await to call an async function and wait for its result.
Fill both blanks to correctly define and call an async function that returns a string.
func getMessage() [1] -> String { return "Hello" } func show() async { let message = [2] getMessage() print(message) }
The function must be marked async to be asynchronous, and the call must use await to wait for its result.
Fill all three blanks to create an async function, call it with await, and handle errors with try.
func fetchInfo() [1] throws -> String { return "Info" } func process() async { do { let info = [2] [3] fetchInfo() print(info) } catch { print("Error") } }
The function is marked async and throws. When calling it, use try and await to handle errors and wait for the result.