Alexandru Hadăr
04/11/2024, 8:21 PMsuspend fun printId() {
val id = getId() // suspend fun getId(): String
saveId(id) // private suspend fun saveId(id: String)
println("Your id is: $id")
}
Roughly, translated by the compiler it would look like:
fun printId(continuation: Continuation<*>) {
val result : Result<Any>? = continuation.result
when (continuation.label) {
0 -> {
continuation.label = 1
val res = getId(continuation)
if (res == COROUTINE_SUSPENDED) {
return COROUTINE_SUSPENDED
}
result = Result.success(res)
}
1 -> {
...
}
}
}
What I don't understand is when will result = Result.success(res)
ever be called?
From what I can tell:
• label is already 1, so when the continuation's resumeWith
inside getUserId
will be called will go directly to 1
• otherwise, getId
could only return COROUTINE_SUSPENDED
, because will move the function to another thread;
Could it be when the we don't use a dispatcher inside getId
, so everything is on the same thread? Any other cases?ephemient
04/11/2024, 8:45 PMAlexandru Hadăr
04/11/2024, 8:54 PM