Interface Promise<C>

  • Type Parameters:
    C - the type of the result value
    All Known Implementing Classes:
    Promise.Completable

    public interface Promise<C>

    The future result of an operation, either a value if the operation succeeded, or a failure if the operation failed.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  Promise.Completable<S>
      A CompletableFuture that is also a Promise.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Promise<?> NOOP
      Shared instance whose methods are implemented empty,
    • Method Summary

      All Methods Static Methods Instance Methods Default Methods 
      Modifier and Type Method Description
      default java.util.function.BiConsumer<C,​java.lang.Throwable> complete()
      Returns a BiConsumer that, when invoked, completes this Promise.
      static <T> Promise<T> complete​(java.util.function.BiConsumer<T,​java.lang.Throwable> fn)
      Returns a Promise that, when completed, invokes the given BiConsumer function.
      default void fail​(java.lang.Throwable failure)
      Callback to invoke when the operation fails.
      static <T> Promise<T> from​(java.util.function.Consumer<T> succeed, java.util.function.Consumer<java.lang.Throwable> fail)  
      static <T> Promise<T> noop()  
      default void succeed​(C result)
      Callback to invoke when the operation succeeds.
    • Field Detail

      • NOOP

        static final Promise<?> NOOP

        Shared instance whose methods are implemented empty,

        use noop() to ease type inference.

    • Method Detail

      • succeed

        default void succeed​(C result)

        Callback to invoke when the operation succeeds.

        Parameters:
        result - the result
        See Also:
        fail(Throwable)
      • fail

        default void fail​(java.lang.Throwable failure)

        Callback to invoke when the operation fails.

        Parameters:
        failure - the operation failure
      • complete

        default java.util.function.BiConsumer<C,​java.lang.Throwable> complete()

        Returns a BiConsumer that, when invoked, completes this Promise.

        Typical usage is with CompletableFuture:

         public void process(ServerMessage message, Promise<Boolean> promise) {
             CompletableFuture.supplyAsync(() -> asyncOperation(message))
                     .whenComplete(promise.complete());
         }
         
        Returns:
        a BiConsumer that completes this Promise
        See Also:
        Promise.Completable
      • noop

        static <T> Promise<T> noop()
        Type Parameters:
        T - the type of the empty result
        Returns:
        a Promise whose methods are implemented empty.
      • from

        static <T> Promise<T> from​(java.util.function.Consumer<T> succeed,
                                   java.util.function.Consumer<java.lang.Throwable> fail)
        Type Parameters:
        T - the type of the result value
        Parameters:
        succeed - the Consumer to call in case of successful completion
        fail - the Consumer to call in case of failed completion
        Returns:
        a Promise from the given consumers
      • complete

        static <T> Promise<T> complete​(java.util.function.BiConsumer<T,​java.lang.Throwable> fn)

        Returns a Promise that, when completed, invokes the given BiConsumer function.

        Type Parameters:
        T - the type of the result value
        Parameters:
        fn - the function to invoke when the Promise is completed
        Returns:
        a Promise that invokes the BiConsumer function