/** * Completes this promise with the specified Promise, once that Promise is completed. * * @param other The value to complete with * @return A promise giving the result of attempting to complete this promise with the other * promise. If the completion was successful then the result will be true, if the * completion couldn't occur then the result will be false. */ public Promise<Boolean> tryCompleteWith(Promise<? extends A> other) { return new Promise<>(other.handle((a, error) -> { if (error != null) { return future.completeExceptionally(error); } else { return future.complete(a); } })); }
/** * Creates a new promise which holds the result of this promise if it was completed successfully, * otherwise the result of the {@code fallback} promise if it completed successfully. * If both promises failed, the resulting promise holds the throwable of this promise. * * @param fallback The promise to fallback to if this promise has failed * @return A promise that will delegate to another promise on failure * Use {@link #handleAsync(BiFunction)} followed by {@link #thenCompose(Function)} * with the identity function. */ public Promise<A> fallbackTo(final Promise<A> fallback) { return new Promise<>(wrapped.handle((a, error) -> { if (error != null) { return fallback.handle((fallbackA, fallbackError) -> { if (fallbackError != null) { CompletableFuture<A> failed = new CompletableFuture<>(); failed.completeExceptionally(error); return failed; } else { return CompletableFuture.completedFuture(fallbackA); } }).thenCompose(Function.identity()); } else { return CompletableFuture.completedFuture(a); } }).thenCompose(Function.identity())); }