func fetchData() async -> String {
print("Start fetching data")
// 模拟异步操作,使用 Task.sleep 等待 2 秒钟
await Task.sleep(2_000_000_000)
print("Data fetching complete")
return "Data"
}
func processData() async {
print("Start processing data")
let data = await fetchData() // 等待 fetchData() 异步操作的完成
print("Data processing complete. Received: \(data)")
}
print("Before calling processData")
Task {
await processData() // 调用异步函数 processData()
}
print("After calling processData")
疑问:
[ol]
[/ol]