What is the difference between ```(1..3).asFlow()...
# coroutines
c
What is the difference between
Copy code
(1..3).asFlow().onEach { number -> println("$number") }.collect()
And
Copy code
(1..3).asFlow().collect { number -> println("$number") }
Why would I want to use
onEach
when I can use
collect
directly?
a
They are equivalent. Some people may prefer one to the other. Or, for example, using
onEach
followed by
launchIn
rather than
collect
😅 1
f
No difference, as you already said.
onEach
is most of the time used in combination with a tail
.launchIn(CoroutineScope)
instead of a surrounding
Coroutinescope.launch { ... }
a
jinx!
c
Oh ok, I wondered If I was missing something, thank you!
j
you have the possibility to call other operators like
onStart
or
catch
when using
onEach
f
you have the possibility to call other operators like
onStart
or
catch
when using
onEach
You can also use those operators with collect
j
right of course, I was thinking of “after” calling
collect
or
onEach
, I should I specified that :)