How are Mono<Void> and Mono.empty() different

Viewed 10639

As per my understanding, in Spring WebFlux reactor

Mono<Void> refers for a void Mono

Mono.empty() refers to void, as calling anything over this gives a null pointer.

How do these stand different in their usage ?

1 Answers

Mono<T> is a generic type - in your specific situation it represents Void type as Mono<Void>

Mono.empty() - return a Mono that completes without emitting any item.

Let's assume that you got a method:

private Mono<Void> doNothing() {
    return Mono.empty();
}

Whe you want to chain anything after the method call it won't work with flatMap as it is a completed Mono. In case you want continue another job after that method you can use operator then:

doNothing().then(doSomething())
Related