title: How to implement CompletableFuture.allOf() that completes exceptionally once any of the futures fail?
body: <p>I want to implement a hybrid of <code>CompletableFuture.allOf()</code> and <code>CompletableFuture.anyOf()</code> where the returned future completes successfully as soon as all of the elements complete successfully, or it completes exceptionally (with the same exception) as soon as any of the elements complete exceptionally. In the case of multiple elements failing, returning the exception of any of them is sufficient.</p>
  
  <h2>Use-case</h2>
  
  <p>I have a task that needs to aggregate sub-results returned by a list of <code>CompletableFuture</code>s, but that task should stop waiting as soon as any of them fails. I understand that the sub-tasks will keep on running and that's okay.</p>
  
  <h2>Related questions</h2>
  
  <p>I found <a href="https://stackoverflow.com/q/19348248/14731">Waiting on a list of Future</a> which initially seems like a duplicate question but the accepted answer uses <code>CompletionService</code> which requires <code>Callable</code> or <code>Runnable</code> as input. I am looking for a solution that takes already-running <code>CompletionStage</code>s as input.</p>