YuhSunMan
YuhSunMan

Reputation: 55

What does it mean for Spring Reactor's Mono to return empty?

From its documentation, it states that Mono returns empty when it "completes without emitting any items". What does it mean to complete without emitting any items? Does it mean that it never sent any request or?

Upvotes: 2

Views: 1279

Answers (3)

Павел
Павел

Reputation: 728

To add to Simon's complete answer (why do I do that, hm?) One typical use case of Mono returning empty is filtering. Think of it like if you need to do something in if then else style, in terms of Mono you could use .filter for then and .switchIfEmpty for else.

To understand the logic behind, for me it was also useful to look onto interface MonoSink<T> apidoc. It basically says the same: you can either complete without value, or with it, see two overloaded success methods.

Upvotes: 1

Simon Basl&#233;
Simon Basl&#233;

Reputation: 28351

Answering from the perspective of "what does it mean in terms of behavior", not "what does the empty Mono behavior means in terms of business logic":

Mono is a Publisher, an interface that is defined to emit a set of signals to its Subscriber: onNext, onComplete, onError.

In the case of Mono the possible combinations are restricted:

  • onNext followed by onComplete (something was produced and we're done)
  • onError (something went wrong)
  • onComplete (nothing was produced but we're done)

The last one is the empty case you're wondering about: an empty Mono is one that never emits the onNext signal but rather simply emits onComplete.

Upvotes: 2

Martin Tarj&#225;nyi
Martin Tarj&#225;nyi

Reputation: 9997

It depends on the implementation. Generally, for reactive data access libraries it means that the request/query was executed but yielded no results. However, this is not always the case as there are alternative behaviours like returning a default value or returning a Mono with error. Always consult the relevant library (e.g.: Spring Data) documentation to understand the behavior.

Upvotes: 1

Related Questions