thenApply
and thenCompose
are methods of CompletableFuture
. Use them when you intend to do something to CompletableFuture
's result with a Function
.
thenApply
and thenCompose
both return a CompletableFuture
as their own result. You can chain multiple thenApply
or thenCompose
together. Supply a Function
to each call, whose result will be the input to the next Function
.
The Function
you supplied sometimes needs to do something synchronously. The return type of your Function
should be a non-Future
type. In this case you should use thenApply
.
CompletableFuture.completedFuture(1)
.thenApply((x)->x+1) // adding one to the result synchronously, returns int
.thenApply((y)->System.println(y)); // value of y is 1 + 1 = 2
Other times you may want to do asynchronous processing in this Function
. In that case you should use thenCompose
. The return type of your Function
should be a CompletionStage
. The next Function
in the chain will get the result of that CompletionStage
as input, thus unwrapping the CompletionStage
.
// addOneAsync may be implemented by using another thread, or calling a remote method
abstract CompletableFuture<Integer> addOneAsync(int input);
CompletableFuture.completedFuture(1)
.thenCompose((x)->addOneAsync(x)) // doing something asynchronous, returns CompletableFuture<Integer>
.thenApply((y)->System.println(y)); // y is an Integer, the result of CompletableFuture<Integer> above
This is a similar idea to Javascript's Promise
. Promise.then
can accept a function that either returns a value or a Promise
of a value. The reason why these two methods have different names in Java is due to generic erasure. Function<? super T,? extends U> fn
and Function<? super T,? extends CompletionStage<U>> fn
are considered the same Runtime type - Function
. Thus thenApply
and thenCompose
have to be distinctly named, or Java compiler would complain about identical method signatures. The end result being, Javascript's Promise.then
is implemented in two parts - thenApply
and thenCompose
- in Java.
You can read my other answer if you are also confused about a related function thenApplyAsync
.
map
andflatMap
inStream
?thenApply
is themap
andthenCompose
is theflatMap
ofCompletableFuture
. You usethenCompose
to avoid havingCompletableFuture<CompletableFuture<..>>
. – Sporogony