Kotlin 2 · Coroutines
Stream values with Flow
A flow emits values over time and is collected from a coroutine.
fun ticks(): Flow<Int> = flow {
for (i in 1..3) {
delay(100)
emit(i)
}
}
ticks().collect { println(it) }
Official Kotlin 2 documentation