public static < T > void completeAfter ( Supplier < CompletableFuture < ? extends T > > futureSupplier , CompletableFuture < T > toComplete ) { Preconditions . checkArgument ( ! toComplete . isDone ( ) , "toComplete is already completed." ) ; try { CompletableFuture < ? extends T > f = futureSupplier . get ( ) ; // Async termination. f . thenAccept ( toComplete :: complete ) ; Futures . exceptionListener ( f , toComplete :: completeExceptionally ) ; } catch ( Throwable ex ) { // Synchronous termination. toComplete . completeExceptionally ( ex ) ; throw ex ; } }
Given a Supplier returning a Future completes another future either with the result of the first future in case of normal completion or exceptionally with the exception of the first future .