Course contentsShow
← Java
Lesson 2829 of 2,87051. CompletableFutureFree lesson

thenCombine vs thenCompose: Key Differences

Compare thenCombine for independent futures with thenCompose for dependent sequential operations.

thenCombine vs thenCompose: Key Differences

You'll learn: How to choose between thenCombine for independent parallel operations and thenCompose for dependent sequential chains.

The Core Distinction

These two methods solve fundamentally different problems in async workflows:

thenCombine is for combining two independent futures that can run in parallel. Think of it like two friends each going to different stores—they shop simultaneously, then meet up to combine their purchases.

thenCompose is for dependent sequential operations where the second task needs the result of the first. Think of it like reading a treasure map, then traveling to the location—you can't do the second until the first completes.

When to Use Each

Use thenCombine When:

  • Both computations can start immediately
  • Neither depends on the other's result
  • You want to maximize parallelism
  • Example: Fetching user data AND fetching weather data simultaneously, then displaying both

Use thenCompose When:

  • The second operation requires the first's result
  • You're chaining async calls sequentially
  • You'd otherwise end up with CompletableFuture<CompletableFuture<T>>
  • Example: Looking up a user ID, then fetching that user's profile details

Quick Decision Guide

Ask yourself: "Does the second operation need to wait for the first's result?"

  • Yes → Use thenCompose (sequential dependency)
  • No → Use thenCombine (parallel independence)

Key Takeaway: Use thenCombine to merge two independent parallel futures with a combining function; use thenCompose to chain dependent async operations sequentially, avoiding nested CompletableFutures.