Skip to content
Richard Hightower edited this page Apr 26, 2016 · 5 revisions

Result is the result of an async operation. This was modeled after Vert.x AsyncResult and after the types of results one would deal with in JavaScript. A Promise is both a Result and a Callback.

Example usage using then and catchError

        employeeService.lookupEmployee(33, result -> {
            result.then(e -> saveEmployee(e))
                  .catchError(error -> {
                    logger.error("Unable to lookup", error);
            });
        });

Example usage using Expected from thenExpected and catchError

        employeeService.lookupEmployee(33, result -> {
            result.thenExpect(Expected -> Expected.ifPresent(e -> saveEmployee(e)))
                  .catchError(error -> {
                    logger.error("Unable to lookup", error);
            });
        });

####Result

package io.advantageous.reakt;

import io.advantageous.reakt.impl.ResultImpl;

import java.util.function.Consumer;

/**
 * The result of an async operation.
 * <p>
 * This was modeled after Vert.x AsyncResult and after the types of results one would deal with in JavaScript.
 *
 * @param <T> type of value expected in the result.
 */
public interface Result<T> {

    /**
     * Create a result
     *
     * @param value value
     * @param <T>   T
     * @return result
     */
    static <T> Result<T> result(T value) {
        return new ResultImpl<>(value);
    }

    /**
     * Create a result
     *
     * @param error error
     * @param <T>   T
     * @return result
     */
    static <T> Result<T> error(Throwable error) {
        return new ResultImpl<>(error);
    }


    /**
     * If a result is sent, and there was no error, then handle the result.
     *
     * @param consumer executed if result has no error.
     * @throws NullPointerException if result is present and {@code consumer} is
     *                              null
     */
    Result<T> then(Consumer<T> consumer);


    /**
     * If a result is sent, and there was no error, then handle the result as a value which could be null.
     *
     * @param consumer executed if result has no error.
     * @throws NullPointerException if result is present and {@code consumer} is
     *                              null
     */
    Result<T> thenExpect(Consumer<Expected<T>> consumer);


    /**
     * If a result is sent, and there is an error, then handle handle the error.
     *
     * @param consumer executed if result has error.
     * @throws NullPointerException if result is present and {@code consumer} is
     *                              null
     */
    Result<T> catchError(Consumer<Throwable> consumer);

    /**
     * @return true if result is sent successfully.
     */
    boolean success();

    /**
     * @return true if result is sent and this is the last result.
     */
    boolean complete();


    /**
     * If failure is true then cause will not be null.
     *
     * @return true if result is sent and result outcome is a failure.
     */
    boolean failure();

    /**
     * If failure is true, the cause will not be null.
     *
     * @return cause of error associated with the result
     */
    Throwable cause();


    /**
     * If the value of the result can be null, it is better to use Expected which is like Optional.
     *
     * @return value associated with a successful result.
     */
    Expected<T> expect();

    /**
     * Raw value of the result.
     * You should not use this if the result could be null, use getExpected instead.
     *
     * @return raw value associated with the result.
     */
    T get();
}

Exceptions and Results (and Promises)

Reakt throws exceptions when you call get() on a result or promise and the result or promise is in an error state.

If the cause is a RuntimeException, then the original exception will be thrown. If the cause is not a RuntimeException, then it will be wrapped in on of the following RuntimeExceptions.

  • RejectedPromiseException - The promise was rejected with a non-runtime Exception.
  • ResultFailedException - The result failed due to a non-runtime Exception.
  • ThenHandlerException - The promise was successful but your then handler or thenExpect handler threw an exception.
Clone this wiki locally