如何避免 .flatMap(x->reactiveAction(x).thenReturn(x))

Neo*_*ith 1 java mono publisher reactive-programming project-reactor

在使用项目反应器库的 Java 响应式编程期间,我偶然发现了一种模式,我想知道是否有开箱即用的支持?

所以我想要下面的代码:

Mono.just("hello")
    .flatMap(hello -> reactiveAction(hello).thenReturn(hello))
    ..
    .;
Run Code Online (Sandbox Code Playgroud)

变成类似的东西:

Mono.just("hello")
    .coolOperation(this::reactiveAction)
    ..
    .;   
Run Code Online (Sandbox Code Playgroud)

我不能使用 doOnNext 因为我想在 reactAction 中做的不是副作用。和反应动作是:

Mono<Integer> reactiveAction(String text){
  return ....
}
Run Code Online (Sandbox Code Playgroud)

bsi*_*eup 6

你考虑过Mono#delayUntil吗?

Mono.just("hello")
    .delayUntil(hello -> reactiveAction(hello))
    ..
    .;
Run Code Online (Sandbox Code Playgroud)

  • `delayUntil` 非常适合您的用例,在使用它之前没有任何需要考虑的:) (2认同)