CompletableFuture recoverWith equivalent? i.e. exceptionally but return CompletableFuture<U>
completable-future, java, java-8
Solution
After a lot of frustration in trying to figure out the proper way of doing Scala's recoverWith in Java 8, I ended up just writing my own. I still don't know if this is the best approach, but I created something like:
public RecoveryChainAsync<T> recoverWith(Function<Throwable,
CompletableFuture<T>> fn);
With repeated calls to recoverWith, I queue up the functions inside the recovery chain and implement the recovery flow myself with "handle". RecoveryChainAsync.getCompletableFuture() then returns a representative CompletableFuture for the entire chain. Hope this helps.
Problem
I don't see an obvious way to handle an exception with an asynchronous result. For example, if I want to retry an async operation, I would expect something like this: ``` CompletionStage<String> cf = askPong("cause error").handleAsync((x, t) -> { if (t != null) { return askPong("Ping"); } else { return x; } }); ``` Where askPong asks an actor: ``` public CompletionStage<String> askPong(String message){ Future sFuture = ask(actorRef, message, 1000); final CompletionStage<String> cs = toJava(sFuture); return cs; } ``` However `handleAsync` doesn't do what you think it does - it runs the callbacks on another thread asynchronously. Returning a `CompletionStage` here is not correct. Jeopardy question of the day: `thenApply` is to `thenCompose` as `exceptionally` is to what?