Ray Zhang
Ray Zhang

Reputation: 1561

Asynchronous Java: How to return this without blocking?

Setup:

public Mono<String> getResult(Mono<Boolean> flagMono, Mono<String> resultMono) {
    return flagMono.map(flag -> {
        if (flag) return "FLAG IS TRUE, SKIP RESULT";
        return resultMono.block(); // how to do without blocking?
    });
}

Hopefully, what I'm trying to accomplish is clear and it's just a matter of writing it out correctly. I would like to return the string constant if flag is true, otherwise return the string result returned by resultMono in a non-blocking way.

Upvotes: 0

Views: 344

Answers (1)

Alexander Pavlov
Alexander Pavlov

Reputation: 2220

public Mono<String> getResult(Mono<Boolean> flagMono, Mono<String> resultMono) {
    return flagMono.flatMap(flag -> {
        if (flag) return Mono.just("FLAG IS TRUE, SKIP RESULT");
        return resultMono;
    });
}

Upvotes: 2

Related Questions