September 2019
Intermediate to advanced
816 pages
18h 47m
English
Let's assume that we have the following two CompletableFuture instances in a helper class named CustomerAsyncs:
private static CompletableFuture<String> fetchOrder(String customerId) { return CompletableFuture.supplyAsync(() -> { return "Order of " + customerId; });}private static CompletableFuture<Integer> computeTotal(String order) { return CompletableFuture.supplyAsync(() -> { return order.length() + new Random().nextInt(1000); });}
Now, we want to fetch the order of a certain customer, and, once the order is available, we want to compute the total of this order. This means that we need to call fetchOrder() and afterward computeTotal(). We can do this via thenApply():
CompletableFuture<CompletableFuture<Integer>> ...