I am trying to understand how JavaRx's Flux.merge and switchIfEmpty work together in regards to the below code as I am a bit confused on results I am seeing which is no doubt the result of my not fully grasping Java RX.
My question is ... If the call to wOneRepository... returns an empty list or the call to wTwoRepository... returns an empty list, will the switchIfEmpty code get executed? Or will it only get executed if both calls return an empty list?
Flux<Widget> f1 = wOneRepository.findWidgets(email).onErrorResume(error -> Flux.empty());
Flux<Widget> f2 = wTwoRepository.findWidgets(email).onErrorResume(error -> Flux.empty());
return Flux.merge(f1, f2)
.switchIfEmpty(Flux.error(new IllegalArgumentException("Widgets were not found")));
Thank you
switchIfEmpty()
will only be called if the upstream Flux
completes without emitting anything, and that will only happen if both f1
and f2
complete without emitting anything. So, if both findWidget
calls fail, or both return empty Flux
instances, or some combination of those, then switchIfEmpty
will be called. If either f1
or f2
emits a Widget
, then that Widget
will be emitted from the merge
operator, which means switchIfEmpty
will not be called.