Scrivio Interview library
CodingFoundations5 min read · updated 12 November 2026

Why does this produce CompletableFuture<CompletableFuture<Order>>, and how do you fix it?

Java
CompletableFuture<User> user = findUser(id);
var order = user.thenApply(u -> findLatestOrder(u));

where findLatestOrder returns CompletableFuture<Order>.

thenApply wraps whatever the function returns in a new future, so when the function itself returns a CompletableFuture<Order> we get a future of a future. thenCompose is made for functions that return a CompletionStage: it waits for that inner stage and flattens the result, so user.thenCompose(u -> findLatestOrder(u)) gives CompletableFuture<Order>. The rule is thenApply for plain values and thenCompose for asynchronous functions, just like map and flatMap.

Why this question is asked

Chaining one async call after another is the most common thing we do with CompletableFuture: find a user, then find their orders, then load the shipment. Getting a nested type like CompletableFuture<CompletableFuture<Order>> is the usual first stumble, and the way people work around it tells an interviewer a lot. In this answer, we will see what thenApply does with a return value, reproduce the nested type, fix it with thenCompose, and connect the rule to map and flatMap, which we already know from streams and Optional.

1A Quick Recap of thenApply

A CompletableFuture<User> is a placeholder for a User that will arrive later. thenApply lets us say what to do with that value once it arrives. We pass a function from User to some type R, and thenApply immediately returns a new CompletableFuture<R> that will hold the function's result.

Java
CompletableFuture<User> user = findUser(42);
CompletableFuture<String> name = user.thenApply(u -> u.name()); // String wrapped in a future
name.join(); // "Ada"

The important detail is the wrapping. Whatever our function returns, thenApply puts it inside a new future. For a String that is exactly what we want.

2Reproducing the Nested Future

Now our function is findLatestOrder, which is itself asynchronous and already returns CompletableFuture<Order>. thenApply does not know or care that the value is a future. It wraps it like any other value.

Java
CompletableFuture<User> user = findUser(42);
var order = user.thenApply(u -> findLatestOrder(u));
// type of order: CompletableFuture<CompletableFuture<Order>>
Order o = order.join(); // does not compile: join() gives CompletableFuture<Order>
Order o2 = order.join().join(); // compiles, but blocks twice and is clumsy

Notice that the outer future completes as soon as findLatestOrder has been called and has returned its future, not when the order has actually arrived. So the outer future tells us very little, and every later stage must unwrap the inner one. Using var hides the odd type until something downstream fails to compile.

3Fix: Use thenCompose to Flatten

thenCompose takes a function that returns a CompletionStage. When the user arrives, it calls our function, gets the inner future back, waits for it without blocking any thread, and completes its own result with the inner future's value. So there is only one level of future.

Java
CompletableFuture<User> user = findUser(42);
CompletableFuture<Order> order = user.thenCompose(u -> findLatestOrder(u));
order.join(); // Order{id=901}
// The chain continues naturally
findUser(42)
.thenCompose(this::findLatestOrder) // async step: thenCompose
.thenApply(Order::total) // plain value: thenApply
.thenAccept(t -> log.info("total {}", t)); // total 59.90
Step throughWhat thenCompose does when the user arrives
  1. findUser completes with uthe first future now has a value
  2. Call findLatestOrder(u)returns an inner CompletableFuture<Order>
  3. Inner future done?
    YesComplete the outer future with the orderNoRegister a callback on the inner future
  4. Inner completes laterthe outer future gets the same order or the same failure

4 steps in order. Play them, or step through them one at a time.

No thread waits: thenCompose links the outer result to the inner one.

Notice that the second chain mixes both methods. findLatestOrder returns a future, so it gets thenCompose. Order::total returns a plain number, so it gets thenApply. If the inner future fails, the outer one fails with the same cause, so errors flow through thenCompose just as they flow through thenApply.

4The Rule, and Its Link to map and flatMap

The rule is simple to remember. If the function returns a plain value, use thenApply. If the function is itself asynchronous and returns a CompletionStage, use thenCompose. A quick check in review is to read the lambda's return type: a future inside thenApply is almost always a mistake.

This is the same idea as map and flatMap. Stream.map with a function returning a Stream gives Stream<Stream<T>>, and flatMap flattens it. Optional.map with a function returning Optional gives Optional<Optional<T>>, and flatMap flattens it. thenApply is the map of CompletableFuture and thenCompose is its flatMap.

Java
Optional<User> ou = Optional.of(user);
ou.map(this::findAddress); // Optional<Optional<Address>>
ou.flatMap(this::findAddress); // Optional<Address>
Stream.of("a b", "c").map(s -> Arrays.stream(s.split(" "))); // Stream<Stream<String>>
Stream.of("a b", "c").flatMap(s -> Arrays.stream(s.split(" "))); // Stream<String>: a, b, c

Once we see the pattern, the nested future stops being a surprise. Any container type that can hold the result of a function has both operations, and the flattening one is what we need when the function returns the same kind of container.

5Follow-up questions

Why not just call join() inside thenApply?

Because it blocks the thread that runs the stage until the inner future finishes. That thread may be a common-pool thread or an HTTP client I/O thread, and blocking it can starve the pool or even deadlock if the inner call needs the same pool. thenCompose gives the same flat result without blocking anything.

Is there an async variant of thenCompose?

Yes. thenComposeAsync(fn) and thenComposeAsync(fn, executor) run our function, the part that starts the inner call, on another thread. That matters only if starting the inner call does real work; the waiting itself never blocks in either form.

What if I already have a nested future I cannot avoid?

We can flatten it after the fact with nested.thenCompose(Function.identity()), which gives a CompletableFuture<Order> without blocking. It is better, though, to fix the stage that created the nesting.

6Conclusion

In this answer, we saw that thenApply wraps the return value of its function in a new future, so an async function inside it produces CompletableFuture<CompletableFuture<Order>>. thenCompose expects a function that returns a CompletionStage and flattens the result, so user.thenCompose(u -> findLatestOrder(u)) gives a plain CompletableFuture<Order>. The rule is thenApply for plain values and thenCompose for asynchronous functions, the same relationship as map and flatMap in streams and Optional.

What to say in the interviewAbout 80 seconds aloud

Open with

The nesting happens because thenApply wraps whatever the function returns in a new future. If the function returns a String, I get a CompletableFuture of String, which is what I want.

Then, if they want more

But findLatestOrder already returns a CompletableFuture of Order, so thenApply wraps that future inside another one, and I end up with a CompletableFuture of a CompletableFuture of Order. The outer future even completes as soon as the inner call has been started, not when the order arrives, so it is not useful.

The fix is thenCompose: user.thenCompose(u -> findLatestOrder(u)). thenCompose expects a function that returns a CompletionStage, waits for that inner stage without blocking a thread, and completes its own future with the inner value, so I get a plain CompletableFuture of Order, and failures of the inner call come through as well.

My rule is thenApply when the function returns a plain value and thenCompose when the function is itself asynchronous. It is exactly the same as map versus flatMap on streams or Optional: map with a function that returns a container gives a nested container, and flatMap flattens it.

I would avoid calling join inside thenApply to flatten it, because that blocks a pool thread.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why does this produce CompletableFuture<CompletableFuture<Order>>, and how do you fix it?": https://scrivio-interview-library.netlify.app/java/completable-future/0ce8ac51de/