thenCombine
thenCombine is a method of CompletableFuture that creates a new stage which completes when two independent computations have finished. It takes another CompletionStage and a BiFunction that combines the two results into a new value.
The typical signature is something like thenCombine(CompletionStage<U> other, BiFunction<? super T, ? super U, ? extends V> fn)
Behavior and threading: if either of the two input futures completes exceptionally, the resulting future completes
Variants and executors: there are asynchronous variants, such as thenCombineAsync, which schedule the combining step to
Common use: thenCombine is useful when two independent asynchronous tasks fetch or compute data, and you need
Example: CompletableFuture<Integer> a = fetchA(); CompletableFuture<Integer> b = fetchB(); CompletableFuture<Integer> sum = a.thenCombine(b, (x, y) -> x + y);
See also: thenAcceptBoth, which consumes both results without producing a combined value.