Search code examples
springspring-webfluxreactor

Spring react process more mono in mono


I have a method like:

public Mono<Double> calculate(OperandDto dto) {
    Mono<Double> dbResult1Mono = mongoReactRepo.findByMyLogic(dto);
    Mono<Double> dbResult2Mono = mongoReactRepo.findByMyOtherLogic(dto);

    // ? Multiply these results (dbResult1Mono * dbResult2Mono), and return with Mono<Double> ?
}

And I would like to multiply 2 Mono objects' result and return a new Mono one in a reactive way.

Can anybody help me how to do that?

Thanks


Solution

  • You can use Flux.combineLatest():

        Mono<Double> dbResult1Mono = Mono.just(2d);
        Mono<Double> dbResult2Mono = Mono.just(5d);
    
       return Flux.combineLatest(dbResult1Mono, dbResult2Mono, (r1, r2) -> r1 * r2).next();