Search code examples
javamonoreactive-programmingproject-reactorpublisher

how to avoid .flatMap(x-> reactiveAction(x).thenReturn(x))


During some reactive programming in Java using project reactor library, I stumbled upon a pattern for which I'm wondering if there is out of box support?

So I want the code below:

Mono.just("hello")
    .flatMap(hello -> reactiveAction(hello).thenReturn(hello))
    ..
    .;

to be turned into something like:

Mono.just("hello")
    .coolOperation(this::reactiveAction)
    ..
    .;   

I can not use doOnNext because what I want to do inside reactiveAction is not side effect. and reactive action is:

Mono<Integer> reactiveAction(String text){
  return ....
}

Solution

  • Have you considered Mono#delayUntil?

    Mono.just("hello")
        .delayUntil(hello -> reactiveAction(hello))
        ..
        .;