如何仅返回Kotlin中两个并发异步挂起函数中的第一个函数的结果?
下面是一个例子 如何更改上述内容,以便只返回第一个完成函数的值如何仅返回Kotlin中两个并发异步挂起函数中的第一个函数的结果?,kotlin,Kotlin,下面是一个例子 如何更改上述内容,以便只返回第一个完成函数的值 suspend fun fasterUseful(): Int = coroutineScope { val one = async { doSomethingUsefulOne() } val two = async { doSomethingUsefulTwo() } // ????? one.await() || two.await() whichever faster
suspend fun fasterUseful(): Int = coroutineScope {
val one = async { doSomethingUsefulOne() }
val two = async { doSomethingUsefulTwo() }
// ????? one.await() || two.await() whichever faster
}
选择{
一.等等{it}
2.等等{it}
}
看<如果您不想赋予第一个子句优先权,请选择无偏见。选择{
一.等等{it}
2.等等{it}
}
看<代码>如果不想赋予第一个子句优先级,请选择无偏见
suspend fun fasterUseful(): Int = coroutineScope {
val one = async { doSomethingUsefulOne() }
val two = async { doSomethingUsefulTwo() }
// ????? one.await() || two.await() whichever faster
}
select<Int> {
one.onAwait { it }
two.onAwait { it }
}