Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mono doOnEmpty in Reactor?

Is there any operator on Mono that would allow me to log fact that mono was empty?

I cannot use hasElement() because I need result and I don't want to introduce hacky solutions like abusing switchIfEmpty

like image 674
pixel Avatar asked Sep 08 '26 21:09

pixel


2 Answers

You could use doOnSuccess and test if data is null

.doOnSuccess(data -> {
    if (data == null) {
        //onEmpty behavior
    }
})
like image 152
Alexander Pankin Avatar answered Sep 12 '26 08:09

Alexander Pankin


The issue that the OP is describing is missing functionality in the module (imo). Whether you do:

.switchIfEmpty(
  Mono.defer(() -> {
    //onEmpty behavior
    return Mono.empty();
  })
)

or what the accepted answer is:

.doOnSuccess(data -> {
  if (data == null) {
    //onEmpty behavior
  }
})

(.doOnEach is similar to .doOnSuccess, except it also fires on error signals)

Neither method name really describes the purpose of the code block, which is to only fire on an empty signal. It makes it more time consuming to browse code when debugging issues.

like image 28
Sean M Avatar answered Sep 12 '26 06:09

Sean M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!