有没有办法使用async let语法对两种不同类型执行异步操作,以便这两种方法并行运行?
我也尝试过使用任务组,但无法弄清楚
func getOptionsAndVariants() async {
do {
async let options: [ProductOption] = NetworkManager.sharedInstance.getProductOptions(product.id.description)
async let variants: [ProductVariant] = NetworkManager.sharedInstance.getProductVariants(product.id)
await try options, variants // Wrong syntax
Run Code Online (Sandbox Code Playgroud)
改变
\nawait try options, variants // Wrong syntax\nRun Code Online (Sandbox Code Playgroud)\n到
\nlet result = (await options, await variants)\nRun Code Online (Sandbox Code Playgroud)\n或者,如果您正在等待的任何一件事也需要try,请将其放在相应的之前await。例如,如果他们都try需要,你会说
let result = (try await options, try await variants)\nRun Code Online (Sandbox Code Playgroud)\n现在 result 是表示返回值的两个值的元组result.0\xe2\x80\x94是options值,result.1是variants值。
重要的是,在两个值并行返回 \xe2\x80\x94之前,它result没有任何价值。 async let
| 归档时间: |
|
| 查看次数: |
240 次 |
| 最近记录: |