September 2019
Intermediate to advanced
816 pages
18h 47m
English
While thenCompose() is useful to chain two dependent CompletableFuture instances, thenCombine() is useful to chain two independent instances of CompletableFuture. When both CompletableFuture instances complete we can continue .
Let's assume that we have the following two CompletableFuture instances:
private static CompletableFuture<Integer> computeTotal(String order) { return CompletableFuture.supplyAsync(() -> { return order.length() + new Random().nextInt(1000); });}private static CompletableFuture<String> packProducts(String order) { return CompletableFuture.supplyAsync(() -> { return "Order: " + order + " | Product 1, Product 2, Product 3, ... "; });}
In order to deliver a customer order, we need to compute ...