/** * Used the result of this promise to create a new one by passing the resulting value into the given mapper. * * @param mapper the mapper to transform the promised value of this promise. * @param <X> the resulting type of the mapper * @return a new promise which will be either contain the mapped value or which fails if either this promise fails * or if the mapper throws an exception. */ @Nonnull public <X> Promise<X> map(@Nonnull final Function<V, X> mapper) { final Promise<X> result = new Promise<>(); mapChain(result, mapper); return result; }
/** * Uses to result of this promise to generate a new promise using the given mapper. * * @param mapper the mapper to transform the promised value of this promise. * @param <X> the resulting type of the mapper * @return a new promise which will be either contain the mapped value or which fails if either this promise fails * or if the mapper throws an exception. */ @Nonnull public <X> Promise<X> flatMap(@Nonnull final Function<V, Promise<X>> mapper) { final Promise<X> result = new Promise<>(); onComplete(new CompletionHandler<V>() { @Override public void onSuccess(V value) throws Exception { try { mapper.apply(value).chain(result); } catch (Exception throwable) { result.fail(throwable); } } @Override public void onFailure(Throwable throwable) { result.fail(throwable); } }); return result; }
final Promise<List<V>> result = new Promise<>();
/** * Transforms a collection of items into a promise for a list of results while invoking an async function for * each item. * * @param input the items to iterate over * @param toPromiseHandler the async function which returns a promise to indicate completion * @param <I> the input type * @param <O> the output type generated by the async function * @return a promise containing the invocation results of the async function for each item in the input */ public static <I, O> Promise<List<O>> sequence(Iterable<I> input, Function<I, Promise<O>> toPromiseHandler) { Promise<List<O>> result = new Promise<>(); List<O> buffer = new ArrayList<>(); processChain(input, toPromiseHandler, (ignored, output) -> buffer.add(output)).onSuccess(() -> result.success( buffer)).onFailure(result::fail); return result; }
/** * Forks the given computation and returns a {@link Promise} for the computed value. * <p> * Forks the computation, which means that the current <tt>CallContext</tt> is transferred to the new thread, * and returns the result of the computation as promise. * <p> * If the executor for the given category is saturated (all threads are active and the queue is full) this * will drop the computation and the promise will be sent a <tt>RejectedExecutionException</tt>. * * @param category the category which implies which executor to use. * @param computation the computation which eventually yields the resulting value * @param <V> the type of the resulting value * @return a promise which will either be eventually supplied with the result of the computation or with an error */ public <V> Promise<V> fork(String category, final Supplier<V> computation) { final Promise<V> result = new Promise<>(); executor(category).dropOnOverload(() -> result.fail(new RejectedExecutionException())).fork(() -> { try { result.success(computation.get()); } catch (Exception t) { result.fail(t); } }); return result; }
/** * Updates the entity in the database asynchronous using a dedicated thread pool. * * @param entity the entity to be written into the DB * @param <E> the type of the entity to update * @return a {@link Promise} handling the update process */ public <E extends Entity> Promise<E> updateAsync(E entity) { Promise<E> promise = new Promise<>(); tasks.executor(ASYNC_UPDATER).start(() -> { try { update(entity); promise.success(entity); } catch (Exception e) { promise.fail(e); } }); return promise; }