...

/

A Function with a State and Resumed Value

A Function with a State and Resumed Value

Learn how the function behaves with a state and when resumed with a value.

A function with a state

If a function has some state (like local variables or parameters) that we need to restore after suspension, this state needs to be kept in the function’s continuation. Let’s consider the following function.

Press + to interact
suspend fun myFunction() {
println("Before")
var counter = 0
delay(1000) // suspending
counter++
println("Counter: $counter")
println("After")
}

Here, counter is needed in two states (for a ...