接口 Promise<D>

类型参数:
D - Type used for done(DoneCallback)
F - Type used for fail(FailCallback)
所有超级接口:
Supplier<D>
所有已知子接口:
Deferred<D>
所有已知实现类:
AbstractPromise, DeferredObject

public interface Promise<D> extends Supplier<D>
Promise interface to observe when some action has occurred on the corresponding Deferred object. A promise object should be obtained from {@link Deferred#promise()), or by using DeferredManager. <pre> <code> Deferred deferredObject = new DeferredObject(); Promise promise = deferredObject.promise(); promise.done(new DoneCallback() { public void onDone(Object result) { // Done! } }); // another thread using the same deferredObject deferredObject.resolve("OK");
作者:
Ray Tsang, Stephan Classen
另请参阅:
  • 方法详细资料

    • state

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

      boolean isPending()
      Queries the state of this promise, returning true iff it is State.PENDING.
      返回:
      true if the current state of this promise is State.PENDING, false otherwise.
      另请参阅:
    • isResolved

      boolean isResolved()
      Queries the state of this promise, returning true iff it is State.RESOLVED.
      返回:
      true if the current state of this promise is State.RESOLVED, false otherwise.
      另请参阅:
    • isRejected

      boolean isRejected()
      Queries the state of this promise, returning true iff it is State.REJECTED.
      返回:
      true if the current state of this promise is State.REJECTED, false otherwise.
      另请参阅:
    • getOrElse

      D getOrElse(D defaultValue)
      get the resolved value, return defaultValue for rejected
      参数:
      defaultValue -
      返回:
    • getOrElse

      D getOrElse(Supplier<D> defaultSupplier)
      get the resolved value, return defaultSupplier‘s result for rejected
      参数:
      defaultSupplier -
      返回:
    • then

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

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

      Promise<D> done(Consumer<? super D> callback)
      This method will register DoneCallback so that when a Deferred object is resolved (Deferred.resolve(Object)), DoneCallback will be triggered. If the Deferred object is already resolved then the DoneCallback is triggered immediately. You can register multiple 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) {
           ...
         }
       });
       
       
      参数:
      callback - the callback to be triggered
      返回:
      this for chaining more calls
      另请参阅:
    • fail

      Promise<D> fail(Consumer<Throwable> callback)
      This method will register FailCallback so that when a Deferred object is rejected (Deferred#reject(Object)), FailCallback will be triggered. If the Deferred object is already rejected then the FailCallback is triggered immediately. You can register multiple 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) {
           ...
         }
       });
       
       
      参数:
      callback - the callback to be triggered
      返回:
      this for chaining more calls
      另请参阅:
      • 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 (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 DoneCallback or 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
           }
         }
       });
       
       
      参数:
      callback - the callback to be triggered
      返回:
      this for chaining more calls
      另请参阅:
    • 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.
      抛出:
      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.
      参数:
      timeout - the maximum time to wait in milliseconds
      抛出:
      InterruptedException - if thread is interrupted while waiting