https://kotlinlang.org logo
#coroutines
Title
# coroutines
c

Carrascado

08/23/2020, 3:34 PM
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

araqnid

08/23/2020, 3:46 PM
They are equivalent. Some people may prefer one to the other. Or, for example, using
onEach
followed by
launchIn
rather than
collect
😅 1
f

flosch

08/23/2020, 3:47 PM
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

araqnid

08/23/2020, 3:47 PM
jinx!
c

Carrascado

08/23/2020, 3:48 PM
Oh ok, I wondered If I was missing something, thank you!
j

jean

08/23/2020, 7:53 PM
you have the possibility to call other operators like
onStart
or
catch
when using
onEach
f

flosch

08/23/2020, 8:00 PM
you have the possibility to call other operators like
onStart
or
catch
when using
onEach
You can also use those operators with collect
j

jean

08/23/2020, 8:17 PM
right of course, I was thinking of “after” calling
collect
or
onEach
, I should I specified that :)
2 Views