This is a one way mapping. If you used to use Guava futures, you probably want to do the java 8 equivalent. This does not mean that the reverse mapping works.
Futures.withFallback(future, asyncFunction)
can be mapped to
stage
.thenApply(v -> CompletableFuture.completedFuture(v))
.exceptionally(asyncFunction)
.thenCompose(stage -> stage)
Guava uses the terms Function
and AsyncFunction
where Async means that the
function returns a new future. (This means that all methods that use a regular
Function
can be implemented with the method that takes an AsyncFunction
and
wraps the value in a Futures.immediateFuture(x)
.)
The equivalent of methods that take an AsyncFunction
in Java 8 is
thenCompose
(but that is only implemented for successful futures, not exceptions).
If you want to transform an exception by returning a different future you have to use a workaround (see below).
There are Async variants of the methods for futures in Java 8 too, but that means something completely different: the function or callback you pass in will just be executed on a different thread.