77
88import java .util .List ;
99import java .util .concurrent .CompletableFuture ;
10+ import java .util .function .BiConsumer ;
1011import java .util .stream .Collectors ;
1112
1213@ Mixin (Util .class )
@@ -28,10 +29,14 @@ public static <V> CompletableFuture<List<V>> combineSafe(List<CompletableFuture<
2829 @ Overwrite
2930 public static <V > CompletableFuture <List <V >> combine (List <CompletableFuture <V >> futures ) {
3031 final CompletableFuture <List <V >> future = Combinators .collect (futures , Collectors .toList ()).toCompletableFuture ();
31- CompletableFuture .allOf (futures .toArray (CompletableFuture []::new )).exceptionally (e -> {
32- future .completeExceptionally (e );
33- return null ;
34- });
32+ BiConsumer <V , Throwable > action = (v , throwable ) -> {
33+ if (throwable != null ) {
34+ future .completeExceptionally (throwable );
35+ }
36+ };
37+ for (CompletableFuture <V > completableFuture : futures ) {
38+ completableFuture .whenComplete (action );
39+ }
3540 return future ;
3641 }
3742
@@ -42,11 +47,15 @@ public static <V> CompletableFuture<List<V>> combine(List<CompletableFuture<V>>
4247 @ Overwrite
4348 public static <V > CompletableFuture <List <V >> combineCancellable (List <CompletableFuture <V >> futures ) {
4449 final CompletableFuture <List <V >> future = Combinators .collect (futures , Collectors .toList ()).toCompletableFuture ();
45- CompletableFuture .allOf (futures .toArray (CompletableFuture []::new )).exceptionally (e -> {
46- future .completeExceptionally (e );
47- futures .forEach (f -> f .cancel (false ));
48- return null ;
49- });
50+ BiConsumer <V , Throwable > action = (v , throwable ) -> {
51+ if (throwable != null ) {
52+ future .completeExceptionally (throwable );
53+ futures .forEach (f -> f .cancel (false ));
54+ }
55+ };
56+ for (CompletableFuture <V > completableFuture : futures ) {
57+ completableFuture .whenComplete (action );
58+ }
5059 return future ;
5160 }
5261
0 commit comments