我最近正在尝试学习 Kotlin 协程,我注意到在返回一堆async
IDE 的映射的情况下,会显示消息说Function returning Deferred with a name that does not end with async
.这是我的代码
runBlocking {
try {
val siteDeferred = async { getSite(order) }
// Place where I get warning-----------| (Function returning Deferred with a name that does not end with Async)
// v
val orderLineDeferred = order.line.map { async { getOrderDetail(it) } }
// Place where I get warning-------------------| (Function returning Deferred with a name that does not end with Async)
// v
val orderLineProductsDeferred = order.line.map { async { getOrderProductInformation(it.productId) } }
val site = siteDeferred.await()
val orderLine = orderLineDeferred.awaitAll()
val orderLineProducts = orderLineProductsDeferred.awaitAll()
} catch (e: Throwable) {
throw Exception(e.message)
}
}
private suspend getOrderDetail(OrderLine orderLine): OrderDetail...
private suspend getSite(Order order): Site ...
private suspend getOrderProductInformation(String productId): Product ...
我在这里错过了什么吗?此外,我想知道这是否是进行异常处理的正确方法,以及是否有办法清理try
块,以便我可以直接获取值,即使这意味着我将不得不在其他方法中使用async
。
函数getSite()重命名为getSiteAsync(),其他函数就是这样修改的。