Predict the output
What does this print?
fun main() = runBlocking {
flowOf(1, 2, 3, 4)
.onEach { print("e$it ") }
.filter { it % 2 == 0 }
.map { it * 10 }
.collect { print("c$it ") }
}Hint 1 of 1
Does a flow process one value at a time or the whole list first?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A e1 e2 c20 e3 e4 c40
Correct. Each value goes through the whole chain before the next one starts.
Why not the others?
- B e1 e2 e3 e4 c20 c40. That would be true for a List; flows are processed one value at a time.
- C c20 c40. onEach runs for every value, even ones filtered out later.
- D Nothing, because onEach doesn't collect. collect at the end starts the flow.