I would like to know which is the official mechanism to do zip
using CompletableFuture
.
So far I just use thenCombine
operator. Here my example.
@Test
public void zip() throws InterruptedException {
CompletableFuture<Either<Integer, String>> completableFuture = CompletableFuture.supplyAsync(this::getValue);
CompletableFuture<Either<Integer, String>> completableFuture1 = CompletableFuture.supplyAsync(this::getValue);
CompletableFuture<Either<Integer, String>> completableFuture2 = CompletableFuture.supplyAsync(this::getValue);
completableFuture
.thenCombine(completableFuture1, (c1, c2) -> new Right<>(c1.right().get() + "|" + c2.right().get()))
.thenCombine(completableFuture2, (c1, c2) -> new Right<>(c1.right().get() + "|" + c2.right().get()))
.whenComplete((result, throwable) -> System.out.println(result.right().get()));
Thread.sleep(2000);
}
For me the use of thenCombine
operator is more like merge
operator of RxJava
.
Any idea if there´s a better way to do it?.
I just want run three process in parallel and the zip the results.
Regards.