Philipp Mayer
10/19/2021, 2:25 PMimport arrow.core.Either
import arrow.core.left
import arrow.core.right
import reactor.core.publisher.Mono
fun doSomething(): Mono<Either<String, Boolean>> = Mono.just(1).flatMap { listingId ->
handle(listingId)
}
fun handle(x: Int): Either<String, Mono<Boolean>> =
if (x == 1) Mono.just(true).right()
else "something else".left()
Basically I return either a mono of some value or a default value which is not a mono (but could be if it makes things easier).
In applyforFinancing
I want to return the result of ::handle
, but that gives me:
Type mismatch.
Required:
Either<String, Mono<Boolean>>
Found:
Mono<(???..???)>
How could one achieve that?
Basically I somehow have to transform an Either<Mono<…>, Mono<…>
to Mono<Either<…,…>
Thanks in advance!Philipp Mayer
10/19/2021, 3:34 PMimport arrow.core.Either
import arrow.core.left
import arrow.core.right
import reactor.core.publisher.Mono
fun applyForFinancing(): Mono<Either<String, Boolean>> = serviceCall2().flatMap { listing ->
handle(listing)
}
fun handle(x: Int): Mono<Either<String, Boolean>> =
if (x == 1) serviceCall1().flatMap { Mono.just(it.right()) }
else Mono.just("something else".left())
fun serviceCall1() = Mono.just(true)
fun serviceCall2() = Mono.just(1)
Any feedback is appreciated, thanks!Cody Mikol
10/19/2021, 6:57 PMCody Mikol
10/19/2021, 7:04 PMPhilipp Mayer
10/19/2021, 7:40 PMCody Mikol
10/19/2021, 7:42 PMCody Mikol
10/19/2021, 7:42 PMCody Mikol
10/19/2021, 7:42 PMPhilipp Mayer
10/19/2021, 8:11 PMPhilipp Mayer
10/19/2021, 8:11 PMsimon.vergauwen
10/20/2021, 9:53 AMsuspend
and only wrap in mono { }
from KotlinX Coroutines Reactor integration to wrap it back into the Project Reactor runtime.simon.vergauwen
10/20/2021, 9:56 AMsuspend fun applyForFinancing(): Either<String, Boolean> {
val x = serviceCall2().await()
handle(x)
}
// Domain in suspend
suspend fun handle(int: x): Either<String, Boolean> {
if(x == 1) serviceCall1().await().right()
else "something else".left()
}
// Library that returns Mono
fun serviceCall1() = Mono.just(true)
fun serviceCall2() = Mono.just(1)
simon.vergauwen
10/20/2021, 9:59 AMMono
you can just wrap in mono { }
from KotlinX Coroutines ReactorPhilipp Mayer
10/20/2021, 10:11 AMPhilipp Mayer
10/20/2021, 10:11 AM