I would like to do something like this:
if (someObject == null) {
return Mono.just(someId)
.flatMap(si -> service.monoVoidMethod(si));
}
return Mono.just(someObject)
.flatMap(so -> service.monoObjectMethod(so)
.flatMap(so2 -> service.monoVoidMethod2(so2)))
it's any way to do it in more 'reactive way', without that if statement? I have tried with Mono.switchIfEmpty, but its turns that both monoVoidMethod and monoVoidMethod2 was called when someObject wasn't null.
return Mono.justOrEmpty(someObject)
.flatMap(so -> service.monoObjectMethod(so)
.flatMap(so2 -> service.monoVoidMethod2(so2)))
.switchIfEmpty(Mono.empty().flatMap(var -> service.monoVoidMethod(si)))
I found a twin topic: Mono switchIfEmpty() is always called
and tried also with Mono.defer, but nothing changed:
return Mono.justOrEmpty(someObject)
.flatMap(so -> service.monoObjectMethod(so)
.flatMap(so2 -> service.monoVoidMethod2(so2)))
.switchIfEmpty(Mono.defer(() -> service.monoVoidMethod(si)))
everything works well, when monoVoidMethod2 and monoVoidMethod1 isnt Void type - but this is not my case. In my system monoVoidMethod2 and monoVoidMethod1 returns http status with empty body.
question from:
https://stackoverflow.com/questions/65944904/mono-switchifempty-is-always-executed 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…