How do I correctly use `either {}` in combination...
# arrow
s
How do I correctly use
either {}
in combination with
flow {}
? when I have a function like this
Copy code
fun list(): Either<Error, Flow<Item>> = either { 
    flow {
        if (someSuspendFun()) this@either.raise(Error)
    emitAll(someList())
    }
}
it says
arrow.core.raise.RaiseLeakedException: 'raise' or 'bind' was leaked outside of its context scope. Make sure all calls to 'raise' and 'bind' occur within the lifecycle of nullable { }, either { } or similar builders.
or is the way to go to make the function suspend and do the check before the flow block?
s
Either needs to stay inside Flow, so
Flow<Either<E, A>>
, or you need to call collect inside
either
itself.
s
I'm not collecting it here, just passing the flow on but would this then be okay
Copy code
suspend fun list(): Either<Error, Flow<Item>> = either { 
if (someSuspendFun()) raise(Error)
someList()
}
or is
Either<E, Flow<A>>
a bad idea in general?
s
No, that works great! It depends on your use-case to be honest. This is perfect if constructing the Flow can go wrong, Flow<Either<E, A>> is great if you need to model error as part of your stream without stopping (Exception).
s
alright, thanks! and yeah in that case I want to stop the entire request if permissions are missing
👍 1