diff --git a/src/main/java/com/pivovarit/collectors/AsyncParallelCollector.java b/src/main/java/com/pivovarit/collectors/AsyncParallelCollector.java index 2bc3d25c..1937f73e 100644 --- a/src/main/java/com/pivovarit/collectors/AsyncParallelCollector.java +++ b/src/main/java/com/pivovarit/collectors/AsyncParallelCollector.java @@ -1,7 +1,6 @@ package com.pivovarit.collectors; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -79,14 +78,14 @@ public Set characteristics() { } private static CompletableFuture> combine(List> futures) { - CompletableFuture[] futuresArray = (CompletableFuture[]) futures.toArray(new CompletableFuture[0]); - CompletableFuture> combined = allOf(futuresArray) - .thenApply(__ -> Arrays.stream(futuresArray).map(CompletableFuture::join)); - - for (CompletableFuture f : futuresArray) { - f.exceptionally(ex -> { - combined.completeExceptionally(ex); - return null; + var combined = allOf(futures.toArray(CompletableFuture[]::new)) + .thenApply(__ -> futures.stream().map(CompletableFuture::join)); + + for (var future : futures) { + future.whenComplete((o, ex) -> { + if (ex != null) { + combined.completeExceptionally(ex); + } }); }