reactor / reactor-core

Non-Blocking Reactive Foundation for the JVM

Home Page:http://projectreactor.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

why some steps dont execute during mono

lizq15 opened this issue · comments

why some steps dont execute during mono

public static void main(String[] args) {
        Mono.just(1)
                .map(a -> test())
                .log()
                .block();
    }

    public static Mono<Integer> test() {
        return Mono.just(2)
                .map(a -> {
                    System.out.println("123");
                    return a;
                });
    }

in above , 123 was not printed in the console.

but if i change above to this:

public static void main(String[] args) {
        Mono.just(1)
                .flatmap(a -> test())
                .log()
                .block();
    }

    public static Mono<Integer> test() {
        return Mono.just(2)
                .map(a -> {
                    System.out.println("123");
                    return a;
                });
    }

123 was printed in the console.
Can anyone explain this to me?

In the first case you're not subscribing to the Mono that is returned from block(), so the map is not executed upon it. In the next one, flatMap performs the subscribing on your behalf. Please consult the reference documentation and javadocs - they explain all these in detail. If you struggle though and don't find the answers there, stackoverflow is the right place to ask questions.