Complete the code to declare an asynchronous function in Swift.
func fetchData() [1] {
// code to fetch data
}In Swift, you declare an asynchronous function by adding the async keyword after the function signature.
Complete the code to call an asynchronous function using await.
func load() async {
let data = try? [1] fetchData()
}To call an asynchronous function and wait for its result, use the await keyword before the function call.
Fix the error in the code by adding the missing keyword to handle asynchronous code.
func process() [1] { let result = try? await fetchData() }
The function calling await must itself be marked async to handle asynchronous code properly.
Fill both blanks to create a task that runs asynchronous code concurrently.
Task.[1] { let data = try? [2] fetchData() }
Use Task.detached to start a new concurrent task, and await to wait for the async function inside it.
Fill all three blanks to define an async function that returns a string after awaiting another async call.
func getMessage() [1] [2] -> String { let data = try? [3] fetchData() return data ?? "No data" }
The function is marked async and throws because it can throw errors. Inside, await is used to wait for the async call.