private fun resume() {
when (val curState = state) {
is State.NotReady -> curState.continuation.resume(Unit)
}
}
override suspend fun yield(value: T) = suspendCoroutine<Unit> { continuation ->
state = when (state) {
is State.NotReady -> State.Ready(continuation, value)
is State.Ready<*> -> throw IllegalStateException("Cannot yield a value while ready")
State.Done -> throw IllegalStateException("Cannot yield a value while done")
}
}
第一次执行resume会启动协程,然后运行到yield,yield运行结束是不是挂起了。然后运行权又回到了调用resume的地方。但是前面不是说挂起需要异步么?这里的返回值是Unit,不需要显示的返回COROUTINE_SUSPENDED么?