函数以不以异步结尾的名称返回Deferred

问题描述 投票:2回答:1

我最近尝试学习Kotlin协程,我注意到在返回async一堆的map的情况下,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也可以直接获取该值。 ]在其他方法中。

kotlin kotlinx.coroutines
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.