angryziber / kotlin-puzzlers

A collection of Kotlin Puzzlers
419 stars 58 forks source link

Suspend function #62

Closed rpuxa closed 4 years ago

rpuxa commented 4 years ago
suspend fun getAny(): Any = COROUTINE_SUSPENDED

suspend fun main() {
    val result = withTimeoutOrNull(100) { getAny() == getAny() }
    println(result)
}

a) true b) false c) null d) None of the above

Correct answer: d (this code never finished). Returning COROUTINE_SUSPENDED (using only in library functions) implies coroutine suspension, even though you use withTimeout() coroutine launcher it will never finish

angryziber commented 4 years ago

Thanks!