public static final Promise race ( Promise ... promises ) { if ( promises == null || promises . length == 0 ) { return Promise . resolve ( ) ; } @ SuppressWarnings ( "unchecked" ) CompletableFuture < Tree > [ ] futures = new CompletableFuture [ promises . length ] ; for ( int i = 0 ; i < promises . length ; i ++ ) { futures [ i ] = promises [ i ] . future ; } CompletableFuture < Object > any = CompletableFuture . anyOf ( futures ) ; return new Promise ( r -> { any . whenComplete ( ( object , error ) -> { try { if ( error != null ) { r . reject ( error ) ; return ; } r . resolve ( ( Tree ) object ) ; } catch ( Throwable cause ) { r . reject ( cause ) ; } } ) ; } ) ; }
Returns a new Promise that is completed when any of the given Promises complete with the same result . Otherwise if it completed exceptionally the returned Promise also does so with a CompletionException holding this exception as its cause .