I'm looking through through Kotlin Coroutines - Co...
# coroutines
a
I'm looking through through Kotlin Coroutines - Continuation and something doesn't seem to make sense. Let's say we have the function:
Copy code
suspend 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:
Copy code
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?
a
never knew of such file. Could be a great starting point. Thank you!