/** * Create a new pure promise, that is, a promise with a constant value from the start. * * @param a the value for the promise * Use {@link CompletableFuture#completedFuture(Object)} instead. */ public static <A> Promise<A> pure(final A a) { return new Promise<>(CompletableFuture.completedFuture(a)); }
/** * Maps this promise to a promise of type <code>B</code>. The function <code>function</code> is applied as * soon as the promise is redeemed. * * @param function The function to map <code>A</code> to <code>B</code>. * @param ec The ExecutionContext to execute the function in. * @return A wrapped promise that maps the type from <code>A</code> to <code>B</code>. * Use {@link #thenApplyAsync(Function, Executor)} */ public <B> Promise<B> map(final Function<? super A, ? extends B> function, Executor ec) { return new Promise<>(wrapped.thenApplyAsync(function, (ec))); }
/** * Creates a Promise that wraps a Scala Future. * * @param future The Scala Future to wrap * Use {@link FutureConverters#toJava(Future)} instead. */ public static <A> Promise<A> wrap(Future<A> future) { return new Promise<>(FutureConverters.toJava(future)); }
/** * Create a Promise which will be redeemed with the result of a given function. * <p> * The function will be run in the default ExecutionContext. * * @param function Used to fulfill the Promise. * Use {@link CompletableFuture#supplyAsync(Supplier, Executor)} instead. */ public static <A> Promise<A> promise(Supplier<A> function) { return new Promise<>(CompletableFuture.supplyAsync(function, Execution.httpPromiseContext())); }
@Override public <U> Promise<U> thenCompose(Function<? super A, ? extends CompletionStage<U>> fn) { return new Promise<>(wrapped.thenCompose(fn)); }
@Override public <U> Promise<U> handleAsync(BiFunction<? super A, Throwable, ? extends U> fn, Executor executor) { return new Promise<>(wrapped.handleAsync(fn, executor)); }
@Override public <U> Promise<U> thenApply(Function<? super A, ? extends U> fn) { return new Promise<>(wrapped.thenApply(fn)); }
@Override public Promise<Void> thenRunAsync(Runnable action, Executor executor) { return new Promise<>(wrapped.thenRunAsync(action, executor)); }
@Override public <U, V> Promise<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super A, ? super U, ? extends V> fn) { return new Promise<>(wrapped.thenCombine(other, fn)); }
@Override public Promise<Void> runAfterBoth(CompletionStage<?> other, Runnable action) { return new Promise<>(wrapped.runAfterBoth(other, action)); }
@Override public Promise<Void> acceptEitherAsync(CompletionStage<? extends A> other, Consumer<? super A> action, Executor executor) { return new Promise<>(wrapped.acceptEitherAsync(other, action, executor)); }
@Override public Promise<Void> thenAcceptAsync(Consumer<? super A> action, Executor executor) { return new Promise<>(wrapped.thenAcceptAsync(action, executor)); }
@Override public Promise<Void> thenRunAsync(Runnable action) { return new Promise<>(wrapped.thenRunAsync(action,Execution.httpPromiseContext())); }
@Override public <U, V> Promise<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super A, ? super U, ? extends V> fn) { return new Promise<>(wrapped.thenCombineAsync(other, fn,Execution.httpPromiseContext())); }
@Override public Promise<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action) { return new Promise<>(wrapped.runAfterBothAsync(other, action,Execution.httpPromiseContext())); }
@Override public <U> Promise<U> applyToEitherAsync(CompletionStage<? extends A> other, Function<? super A, U> fn) { return new Promise<>(wrapped.applyToEitherAsync(other, fn,Execution.httpPromiseContext())); }
@Override public <U> Promise<U> thenApplyAsync(Function<? super A, ? extends U> fn) { return new Promise<>(wrapped.thenApplyAsync(fn,Execution.httpPromiseContext())); }
@Override public Promise<Void> thenAcceptAsync(Consumer<? super A> action) { return new Promise<>(wrapped.thenAcceptAsync(action,Execution.httpPromiseContext())); }
@Override public Promise<Void> acceptEitherAsync(CompletionStage<? extends A> other, Consumer<? super A> action) { return new Promise<>(wrapped.acceptEitherAsync(other, action,Execution.httpPromiseContext())); }
/** * Combines the current promise with <code>another</code> promise using `or`. * * @param another promise that will be combined * Use {@link #applyToEither(CompletionStage, Function)} instead. */ public <B> Promise<Either<A, B>> or(Promise<B> another) { return new Promise<>(wrapped.thenApply(Either::<A, B>Left) .applyToEither(another.thenApply(Either::<A, B>Right), Function.identity())); }