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

1

25

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 ?

Neath answered 15/5, 2020 at 15:27 Comment(1)
Mono<Void> is a type. Mono.empty() is a method invocation that returns a Mono that that completes without emitting any item.Curtilage
E
31

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())
Erigeron answered 15/5, 2020 at 18:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.