I have two methods that allows to get an access to underlying storage in async manner
private Mono<String> read(String key) { }
private Mono<Boolean> delete(String key) { }
I want to create another async method that will read value and delete it immediately with returning of read value. I was able to manage it in a really ugly way.
public Mono<String> readAndDelete(String key) {
Mono<String> read = read(key).cache();
return read.then(delete(key)).then(read);
}
But I’m sure more elegant and correct way to do it have to exist. How I can achieve it?
Beside answer proposed by Auktis, similar effect can be achieved with delayUntil method
public Mono<String> readAndDelete(String key) {
return read(key).delayUntil(value -> delete(key));
}
In accordance to documentation delayUnit resolves mono, then triggers another mono specified as an argument and finally returns the result of the first resolved mono.
Advertisement
Answer
Here is a proposition of a − I hope − more elegant way:
public Mono<String> readAndDelete(String key) {
return read(key)
.flatMap(value -> delete(key)
.thenReturn(value)
);
}