Learning Kotlin Programming
Kotlin’s Flow, ChannelFlow, and CallbackFlow Made Easy
Understand flow and its variants for appropriate applications
Published in
9 min readOct 16, 2022
Sometimes early last year, I explore to understand Flow and compare it to Sequence. Flow allows a stream of data handled asynchronously.
Now, other than Flow, we also have ChannelFlow and CallbackFlow. How are they different from Flow?
In order to learn about it, let’s start looking into basic Flow first
Flow
Let’s have this simple code of emitting values from 1 to 5.
fun main() = runBlocking {
flow {
for (i in 1..5) {
println("Emitting $i")
emit(i)
}
}
.collect { value ->
delay(100)
println("Consuming $value")
}
}
The result is as follows
Emitting 1
Consuming 1
Emitting 2
Consuming 2
Emitting 3
Consuming 3
Emitting 4
Consuming 4
Emitting 5
Consuming 5