【发布时间】:2019-07-31 18:43:01
【问题描述】:
我有一个异步CoroutineScope,其中可能是(按条件)对子函数的调用,该子函数以异步Unit返回其结果
如何等待返回的结果并将其返回到异步Unit 之外。因此等待子函数对Unit的调用。
例子:
GlobalScope.launch {
var value: Int = 0
if (condition) {
// the subFunction has a Unit<Int> as return type
subFunction() { result ->
value = result
}
}
Log.v("LOGTAG", value.toString())
}
如何等待subFunction完成执行后再继续执行代码,或者直接将结果值赋给变量?
subFunction 不得是 suspend 函数,但它可以嵌入到辅助函数中。
(代码必须在 Android 环境中运行)
【问题讨论】:
-
如果没有挂起功能,您将无法做到这一点。
-
@Francesc 是否有可能创建一个本地挂起函数来处理这个问题?我无法修改
subFunction
标签: android kotlin kotlin-coroutines