CompletableFuture thenApply vs thenCompose in Java
Codemia Knowledge Hub ·
thenApply transforms the result of a CompletableFuture with a synchronous function. thenCompose chains the result into another asynchronous operation that itself returns a CompletableFuture. It is the same split as map vs flatMap on streams and optionals: thenApply wraps the return value in a future, while thenCompose flattens a nested CompletableFuture<CompletableFuture<T>> into a single CompletableFuture<T>.
thenApply: synchronous transformation
thenApply takes a Function<T, R> and produces a CompletableFuture<R>. The function runs when the upstream future completes, and its return value becomes the result of the new future.
The function passed to thenApply returns a plain value, not a CompletableFuture, and Java wraps that value in a future for you. Use it when the transformation does not involve I/O, network calls or other asynchronous work.
Chaining several thenApply calls
You can chain thenApply calls into a pipeline of synchronous transformations:
Each step runs after the previous one completes, and the result flows through the chain without nesting.
thenCompose: asynchronous chaining
thenCompose takes a Function<T, CompletionStage<R>> and produces a CompletableFuture<R>. The function returns a future, and thenCompose flattens it so you do not end up with a CompletableFuture<CompletableFuture<R>>.
If you use thenApply with a function that returns a CompletableFuture, you get nested futures:
The nested type is unwieldy and forces an extra join() or get() to unwrap. thenCompose exists to avoid this.
The map vs flatMap analogy
If you have used Java streams or optionals, the pattern is familiar:
| Concept | Stream | Optional | CompletableFuture |
| Transform value | map | map | thenApply |
| Flatten nested wrapper | flatMap | flatMap | thenCompose |
| Resulting type (map) | Stream<R> | Optional<R> | CF<R> |
| Resulting type (flatMap) | Stream<R> | Optional<R> | CF<R> |
The choice then becomes mechanical: if your function returns a plain value, use thenApply; if it returns a CompletableFuture, use thenCompose.
Example: chaining service calls
Consider an order processing pipeline where each step calls an external service:
thenCompose handles each step that calls an external service (returns a future), and thenApply handles the purely computational step that builds the confirmation object.
Async variants
Both methods have async counterparts that run the function on a different thread from the ForkJoinPool, or on a custom executor:
Use the async variants when the transformation or composed function is CPU-intensive, or when you need to control which thread pool does the work. For lightweight transformations, the non-async versions are enough and skip the overhead of task submission.
Exception handling
Both thenApply and thenCompose propagate exceptions. If the upstream future completes exceptionally, the downstream function is never called and the exception flows to the next stage. Handle it with exceptionally, handle or whenComplete:
handle is more flexible because it receives both the result and the exception:
Quick reference
| Aspect | thenApply | thenCompose |
| Function signature | Function<T, R> | Function<T, CompletionStage<R>> |
| Return type | CompletableFuture<R> | CompletableFuture<R> (flattened) |
| Analogous to | Stream.map, Optional.map | Stream.flatMap, Optional.flatMap |
| Use when | Transformation is synchronous | Next step is itself asynchronous |
| Async variant | thenApplyAsync | thenComposeAsync |
| Exception behavior | Skipped if upstream fails | Skipped if upstream fails |
Mistakes to avoid
Using thenApply when the function returns a CompletableFuture. This is the most frequent mistake. The result becomes CompletableFuture<CompletableFuture<T>>, which is awkward and usually signals a bug. If your lambda returns a future, use thenCompose.
Blocking inside thenApply or thenCompose. Calling .get() or .join() inside the function defeats asynchronous chaining. It blocks the ForkJoinPool thread and can cause thread starvation under load. Keep the chain non-blocking end to end.
Ignoring the executor for CPU-heavy transformations. The non-async thenApply runs on whatever thread completed the previous stage. If that was a shared pool (like the common ForkJoinPool), a slow transformation can starve other tasks. Use thenApplyAsync with a dedicated executor for expensive work.
Forgetting exception propagation. A failure in any stage silently skips all later thenApply and thenCompose calls until an exceptionally or handle block catches it. Add error handling at the end of the chain, or at intermediate points where recovery is possible.
Confusing thenCompose with thenCombine. thenCompose is sequential: the second future starts after the first completes. thenCombine runs two futures in parallel and combines their results. Using thenCompose for two independent operations wastes time by serializing them.
Related reading
- CompletableFuture vs Async
- CompletableFuture vs Spring Transactions
- CompletableFutureT class join vs get
- Completion handlers and return values
- Computational Complexity of TreeSet methods in Java
- Concatenating null strings in Java
- Concept of promises in Java
- Concurrency Atomic and volatile in C11 memory model
System design interview problems on this topic
- Design a Distributed Locking System: solution and walkthrough
- Design a Distributed Counter: solution and walkthrough
- Design Ticketmaster: solution and walkthrough
- Design a Flash Sale System: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.