Java8 CompletableFuture recoverWith equivalent? eg exceptionally but return CompletableFuture<U>
Is this what you are looking for?
askPong("cause error")
.handle( (pong, ex) -> ex == null
? CompletableFuture.completedFuture(pong)
: askPong("Ping")
).thenCompose(x -> x);
Also, do not use the ...Async
methods unless you intend for the body of the supplied function to be executed asynchronously. So when you do something like
.handleAsync((x, t) -> {
if (t != null) {
return askPong("Ping");
} else {
return x;
})
You are asking for the if-then-else
to be run in a separate thread. Since askPong
returns a CompletableFuture
, there's probably no reason to run it asynchronously.