我有一个条件语句,Deferred在这两种情况下都应该返回结果,但我不知道如何手动创建Deferred具有预定义结果的对象。这是代码快照:
val deferredResult = if (condition) {
callSuspendFunction(params)
} else {
deferred???
}
Run Code Online (Sandbox Code Playgroud)
callSuspendFunction返回运算结果async。虽然我可以写类似coroutineScope { async { result } }而不是的东西deferred???,但它看起来确实不应该是这样。
Ren*_*ene 12
您可以使用 a CompletableDeferred,请参阅https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-completable-deferred/
val value = if(true) {
callSuspendFunction(params)
} else {
CompletableDeferred(10)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4063 次 |
| 最近记录: |