【发布时间】:2022-08-18 20:04:58
【问题描述】:
假设有一些像这样的链接挂起函数。
suspend fun getData(): Boolean {
return request().also {
delay(1000)
}
}
suspend fun request(): Boolean {
return call()
}
suspend fun call(): Boolean {
return run {
delay(1000)
true
}
}
以上工作正常。但是如果我们将also 块转换为Unit 参数,我们会得到一个错误:Suspension functions can be called only within coroutine body
suspend fun getData(): Boolean {
return request {
delay(1000) //the error
}
}
suspend fun request(action: (Boolean) -> Unit): Boolean {
return call().also(action)
}
为什么会这样,有没有可能让Unit继承协程体?