将 Spring Webflux Mono 转换为 Either,最好不阻塞?

Joh*_*han 6 kotlin project-reactor spring-webflux arrow-kt

我正在使用KotlinArrow以及. 我想做的是将Mono实例转换为Eitherspring-webflux

当响应成功或返回错误时Either调用创建实例。Either.right(..)WebClientEither.left(..)WebClient

我正在寻找一种Mono类似于Either.fold(..)的方法,我可以在其中映射成功和错误的结果,并返回与Mono. 像这样的东西(伪代码不起作用):

val either : Either<Throwable, ClientResponse> = 
                   webClient().post().exchange()
                                .fold({ throwable -> Either.left(throwable) },
                                      { response -> Either.right(response)})
Run Code Online (Sandbox Code Playgroud)

一个人应该怎样走呢?

Sim*_*slé 8

没有fold方法,Mono但您可以使用两种方法实现相同的目的:maponErrorResume。事情会是这样的:

val either : Either<Throwable, ClientResponse> = 
               webClient().post()
                          .exchange()
                          .map { Either.right(it) }
                          .onErrorResume { Either.left(it).toMono() }
Run Code Online (Sandbox Code Playgroud)