Reactor - Use orelse after a filter

Asked

Viewed 20 times

0

With the following code:

public void deleteOrSave() {
    Mono.just("someName")
        .filter(this::isMyName)
        .flatMap(this::delete)
        .orElse(this::save);
}


private boolean isMyName(String name) {
    return false;
}

private Mono<Void> delete(String name) {
    return Mono.empty();
}

private Mono<Void> save(String name) {
    return Mono.empty();
}

This orelse is not possible (nor orElseGet), but I would like to implement something similar. Where I have a Mono, filter and if true, I call one method, if not, call another. This is possible ?

1 answer

0

Has the method switchIfEmpty that does this.

Mono.just("someName")
         .filter(this::isMyName)
         .flatMap(this::delete)
         .switchIfEmpty(save("otherName"));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.