How to use a void method in a Java Reactor chain?

Coli :

Consider this code:

import reactor.core.publisher.Mono;

public class Main {

    public static void main(String[] args) {
        Mono.just(1)
            .map(Main::return_int) // is ok
//          .map(Main::return_void) // is not ok
            .subscribe();
    }

    private static void return_void(int input) {
        // do stuff with input
    }

    private static int return_int(int input) {
        return input;
    }
}

Obviously it's forbidden to use Mono#map with a void param, I get error: method map in class Mono<T> cannot be applied to given types; (...)

Now how can I call this method return_void() in a chain ?

Should I use another operator than #map? Or is there no other choice than wrapping return_void() into a method that returns Mono< Void> ?

bsideup :

There is Mono#doOnNext that does not transform the flow, but allows you to perform side effects (something that returns void, as in your case)

Also, consider using Mono#handle to either proceed or call sink.error(...) when the value does not satisfy your condition instead of throwing from your void function.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=155359&siteId=1