Skip to content
Advertisement

How to chain reactive calls correctly and sequentially

I am trying to make calls with the following order:

  1. save an object

  2. publish an object creation event, only if the first step is done

  3. Return a Flux list What I have currently is following:

    return dbSaveObject(object) //this returns Mono of created object
        .doOnSuccess(s -> hermes.publishEvent(CREATE)) //publishEvent() here returns Mono<Void>
        .thenMany(Flux.just(object))
    

Would this work and publish event as requested, or should I use zipWhen() instead of doOnSuccess()?

Advertisement

Answer

doOn... are so-called side-effect operators and should not be used for constructing reactive flows.

In case publishEvent returns Mono<Void> you could use the following

return dbSaveObject(object)
        .flatMap(savedObject ->
                hermes.publishEvent(CREATE)
                        .thenReturn(savedObject)
        );
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement