Complete the code to start a simple asynchronous task in Swift.
Task [1] { print("Hello from async task!") }
In Swift, Task.detached starts a new asynchronous task detached from the current context.
Complete the code to await an asynchronous function call.
func fetchData() async -> String {
return "Data"
}
func load() async {
let result = [1] fetchData()
print(result)
}The await keyword is used to wait for an asynchronous function to complete in Swift concurrency.
Fix the error in the code to correctly define an async function.
func process() [1] -> String { return "Done" }
To define an asynchronous function in Swift, add the async keyword before the return type.
Fill both blanks to create a Task that returns a value asynchronously.
let task = Task [1] { return [2] }
.init instead of .detached for creating a detached task.Task.detached creates a new detached task, and the task returns the string "Hello".
Fill all three blanks to define and call an async function correctly.
func greet() [1] -> String { return "Hi!" } Task [2] { let message = [3] greet() print(message) }
The function is marked async, the task is created with detached, and the call uses await to wait for the result.