...

/

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.

We'll cover the following...

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.

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 label equal to 0 and 1), so it ...