Aditya Rewari
Aditya Rewari

Reputation: 2687

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

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 ?

Upvotes: 25

Views: 31794

Answers (1)

mslowiak
mslowiak

Reputation: 1838

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();
}

When you want to chain anything after the method call that returns Mono.empty() 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())

Upvotes: 31

Related Questions