Is using `flow.first { .. }` to suspend till a va...
# coroutines
u
Is using
flow.first { .. }
to suspend till a value I want is emitted and then continue in “suspend functions world” with my logic more idiomatic than
take(1)
(and keeping the logic reactive)? or rather is there some technical difference between
flow.filter { … }
and
flow.first { .. }
? I mean they both “wait” by suspending the coroutine, right?
s
The 'filter' method is not terminating and returns another Flow. The 'first' method is terminating returns the emitted value.
By 'terminating' I mean that the source Flow starts being collected and it may start emitting values
u
yea here is a better example
Copy code
flow
	.filter { .. }
	.take(1)
	.collect {
		// logic
	}

vs

flow.first { .. }
// logic
d
"first" enforces at least one element is emitted. Take doesn't.
u
Okay, doesnt matter, Im more talking about the idiomacy of switching from reactive to sequential suspend functions
s
That's a matter of taste. 😀 I don't think there's an overall consensus of one over the other. When possible, I'd prefer sequential over reactive and 'suspend' allows me to do that..
1
d
Not using collect when a more specific function works is more idiomatic.
1