Interface Promise<D>

Type Parameters:
D - 用于成功回调的数据类型
All Superinterfaces:
Supplier<D>
All Known Subinterfaces:
Deferred<D>
All Known Implementing Classes:
AbstractPromise, DeferredObject

public interface Promise<D> extends Supplier<D>
Promise接口,用于观察对应Deferred对象上发生的操作

Promise对象应该从Deferred.promise()获取,或通过DeferredManager使用。

 
 Deferred<String> deferredObject = new DeferredObject<>();
 Promise<String> promise = deferredObject.promise();
 promise.done(result -> {
     // 处理成功结果
 });

 // 另一个线程使用同一个deferredObject
 deferredObject.resolve("OK");
 
 
Author:
Ray Tsang, Stephan Classen
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static enum 
    Promise的状态
  • Method Summary

    Modifier and Type
    Method
    Description
    always(AlwaysCallback<? super D> callback)
    This method will register AlwaysCallback so that when a Deferred object is either resolved (Deferred.resolve(Object)) or rejected (
    invalid reference
    Deferred#reject(Object)
    ), AlwaysCallback will be triggered.
    done(Consumer<? super D> callback)
    This method will register
    invalid reference
    DoneCallback
    so that when a Deferred object is resolved (Deferred.resolve(Object)),
    invalid reference
    DoneCallback
    will be triggered.
    fail(Consumer<Throwable> callback)
    This method will register
    invalid reference
    FailCallback
    so that when a Deferred object is rejected (
    invalid reference
    Deferred#reject(Object)
    ),
    invalid reference
    FailCallback
    will be triggered.
    getOrElse(D defaultValue)
    get the resolved value, return defaultValue for rejected
    getOrElse(Supplier<D> defaultSupplier)
    get the resolved value, return defaultSupplier‘s result for rejected
    boolean
    Queries the state of this promise, returning true iff it is State.PENDING.
    boolean
    Queries the state of this promise, returning true iff it is State.REJECTED.
    boolean
    Queries the state of this promise, returning true iff it is State.RESOLVED.
    状态
    then(Consumer<? super D> doneCallback)
    Equivalent to done(DoneCallback)
    then(Consumer<? super D> doneCallback, Consumer<Throwable> failCallback)
    void
    This method will wait as long as the State is Pending.
    void
    waitSafely(long timeout)
    This method will wait when the State is Pending, and return when timeout has reached.

    Methods inherited from interface java.util.function.Supplier

    get
  • Method Details

    • state

      Promise.State state()
      状态
      Returns:
      the state of this promise.
    • isPending

      boolean isPending()
      Queries the state of this promise, returning true iff it is State.PENDING.
      Returns:
      true if the current state of this promise is State.PENDING, false otherwise.
      See Also:
    • isResolved

      boolean isResolved()
      Queries the state of this promise, returning true iff it is State.RESOLVED.
      Returns:
      true if the current state of this promise is State.RESOLVED, false otherwise.
      See Also:
    • isRejected

      boolean isRejected()
      Queries the state of this promise, returning true iff it is State.REJECTED.
      Returns:
      true if the current state of this promise is State.REJECTED, false otherwise.
      See Also:
    • getOrElse

      D getOrElse(D defaultValue)
      get the resolved value, return defaultValue for rejected
      Parameters:
      defaultValue - 默认值
      Returns:
      如果Promise已解决则返回解决的值,否则返回默认值
    • getOrElse

      D getOrElse(Supplier<D> defaultSupplier)
      get the resolved value, return defaultSupplier‘s result for rejected
      Parameters:
      defaultSupplier - 默认值提供者
      Returns:
      如果Promise已解决则返回解决的值,否则返回默认值
    • then

      Promise<D> then(Consumer<? super D> doneCallback)
      Equivalent to done(DoneCallback)
      Parameters:
      doneCallback - see done(DoneCallback)
      Returns:
      this for chaining more calls
    • then

      Promise<D> then(Consumer<? super D> doneCallback, Consumer<Throwable> failCallback)
      Parameters:
      doneCallback - see done(DoneCallback)
      failCallback - see fail(FailCallback)
      Returns:
      this for chaining more calls
    • done

      Promise<D> done(Consumer<? super D> callback)
      This method will register
      invalid reference
      DoneCallback
      so that when a Deferred object is resolved (Deferred.resolve(Object)),
      invalid reference
      DoneCallback
      will be triggered. If the Deferred object is already resolved then the
      invalid reference
      DoneCallback
      is triggered immediately. You can register multiple
      invalid reference
      DoneCallback
      by calling the method multiple times. The order of callback trigger is based on the order they have been registered.
       
       promise.progress(new DoneCallback(){
         public void onDone(Object done) {
           ...
         }
       });
       
       
      Parameters:
      callback - the callback to be triggered
      Returns:
      this for chaining more calls
      See Also:
    • fail

      Promise<D> fail(Consumer<Throwable> callback)
      This method will register
      invalid reference
      FailCallback
      so that when a Deferred object is rejected (
      invalid reference
      Deferred#reject(Object)
      ),
      invalid reference
      FailCallback
      will be triggered. If the Deferred object is already rejected then the
      invalid reference
      FailCallback
      is triggered immediately. You can register multiple
      invalid reference
      FailCallback
      by calling the method multiple times. The order of callback trigger is based on the order they have been registered.
       
       promise.fail(new FailCallback(){
         public void onFail(Object rejection) {
           ...
         }
       });
       
       
      Parameters:
      callback - the callback to be triggered
      Returns:
      this for chaining more calls
      See Also:
      • invalid reference
        Deferred#reject(Object)
    • always

      Promise<D> always(AlwaysCallback<? super D> callback)
      This method will register AlwaysCallback so that when a Deferred object is either resolved (Deferred.resolve(Object)) or rejected (
      invalid reference
      Deferred#reject(Object)
      ), AlwaysCallback will be triggered. If the Deferred object is already resolved or rejected then the AlwaysCallback is triggered immediately. You can register multiple AlwaysCallback by calling the method multiple times. The order of callback trigger is based on the order they have been registered. AlwaysCallbacks are triggered after any
      invalid reference
      DoneCallback
      or
      invalid reference
      FailCallback
      respectively.
       
       promise.always(new AlwaysCallback(){
         public void onAlways(State state, Object result, Object rejection) {
           if (state == State.RESOLVED) {
             // do something with result
           } else {
             // do something with rejection
           }
         }
       });
       
       
      Parameters:
      callback - the callback to be triggered
      Returns:
      this for chaining more calls
      See Also:
    • waitSafely

      void waitSafely() throws InterruptedException
      This method will wait as long as the State is Pending. This method will return fast when State is not Pending.
      Throws:
      InterruptedException - if thread is interrupted while waiting
    • waitSafely

      void waitSafely(long timeout) throws InterruptedException
      This method will wait when the State is Pending, and return when timeout has reached. This method will return fast when State is not Pending.
      Parameters:
      timeout - the maximum time to wait in milliseconds
      Throws:
      InterruptedException - if thread is interrupted while waiting