I get that I'm in a single-thread environment, and...
# javascript
e
I get that I'm in a single-thread environment, and it's probably the cause.. from my playing around the behaviour I'm seeing is that "if a suspend function itself calls a suspend function, it's just going to block." But if that's true, then doesn't it just mean
Flow.single()
is broken in kotlin-js? Makes me think I'm probably not understanding correctly what's going on.
a
no, a suspend function calls another suspend function fine - the caller enters a state that represents the call so that the execution can resume all the way down the call tree
This worked for me fine on jsIr and jsLegacy:
Copy code
val flow1 = flowOf("red")
    val flow2 = flowOf(1)

    val singlePair = flow1.zip(flow2, ::Pair).single()
e
oh ok, I need to figure out what I'm doing wrong.. (I'm on jsLegacy I believe)
thank you!
a
it does seem odd that the collect collected something there, but the single didn’t produce anything
you’re on jsLegacy unless you’ve taken steps to enable the IR compiler, yes
e
yeah I'm really stumped. that "inner suspend blocks" behaviour is happening elsewhere in my setup so..
I'll investigate
a
can you attach a debugger and see where it’s hanging? it doesn’t make sense for it to actually block, but maybe something’s caught in an infinite loop
e
ok I'll check that..
your example works fine if I try it out at the top-level of my app. Still seeing my issue, stepping through the code hasn't really shown me a smoking gun in my setup, but now I know how where to start 🙂 thank you.
a
one possibility is you’re getting an error there (e.g. one of the flows to be zipped was empty), and the error isn’t caught and is causing the coroutine to be cancelled. That can produce surprising “why did it just suddenly stop there?” behaviour, IME
e
The problem was upstream, I ended up sourcing a
callbackFlow{}
, which doesn't terminate on its own. So
single()
would never stop collecting.
first()
would have worked I believe. But in my specific case using
callbackFlow{}
was the wrong idea.
thank you for your help 🙂
a
glad to hear you worked it out!