How can I combine two eithers into a third that's ...
# arrow
a
How can I combine two eithers into a third that's a result of a suspend function? I tried Either.applicative.map, but apparently the combine block doesn't allow a suspend function to be executed, what's the best way to tackle that?
Copy code
Either.applicative().map(sectionsResult, sectionsResult.map(this::gatherAllSaleIds)) {
                    (sections: List<SalesSectionsQuery.SalesSection>, saleIds: List<SaleId>) ->
// This is a suspend function   remoteSalesDataSource.sales(AFFILIATE, saleIds)
            }
I can do a runBlocking inside that, but not sure if that's a good practice.
a
Let me take a look 🙂
Something like this should work:
Copy code
Either.applicative<String>().map(sectionsResult, sectionsResult.map(::gatherAllSaleIds), ::identity)
    .flatMap { Either.catch { getSales(AFFILIATE, it.b) } }
So, the thing is Either is not really fancy when dealing with effects as suspend functions because is not really meant to that. For that we normally use IO which is very powerful in that matter
the only problem right now is that IO<Either<A, B>> is a bit cumbersome to use as it’s not very friendly to play with, but on 0.11 IO<E, A> will be released so this kind of work can be almost directly translated and optimized for IO
a
Thanks that looks interesting! I looked on IO but unfortunately it only receives one type, and doesn't take the domain error as the left or right parameter type, and 0.11 seems to be not coming soon.
a
yep, we’re working on that still
but yeah, for now you can use Either and try IO at a latter point
here’s another, simpler, approach with Fx
Copy code
Either.fx {
    val ids = sectionsResult.map(::gatherAllSaleIds)
    !Either.catch { getSales(AFFILIATE, ids) }
}
🙏 1
👍 2