https://kotlinlang.org logo
#flow
Title
# flow
m

miqbaldc

03/15/2022, 5:02 AM
Does it Flow extensions
single
is equivalent to
first
? There’s seems a weird issue in my part. When using
single
it returns
null
, but exist otherwise with
first
n

Nick Allen

03/15/2022, 5:19 AM
single
will wait for the
Flow
to finish in the successful case and throw if there was more than just the one item unlike
first
.
today i learned 1
🙏 1
e

ephemient

03/15/2022, 7:59 AM
it doesn't wait for the flow to finish, it just waits to confirm whether the flow has a single element or not. e.g.
Copy code
flow {
    emit(1)
    emit(2)
    delay(1_000_000)
}.single()
doesn't wait for the delay because it can throw as soon as 2 is emitted
K 1
today i learned 1
✍️ 1
🙏 1
this is the same as the stdlib
single
function on Iterable and Sequence
Copy code
sequence {
    yield(1)
    yield(2)
    println("unreachable")
}.single()
n

Nick Allen

03/15/2022, 3:47 PM
Thanks for the clarification. I updated my comment above (the italics part) to be clearer.