接口 Promise<D>
- 类型参数:
D- Type used fordone(DoneCallback)F- Type used forfail(FailCallback)
- 所有超级接口:
Supplier<D>
- 所有已知子接口:
Deferred<D>
- 所有已知实现类:
AbstractPromise,DeferredObject
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
- 另请参阅:
-
Deferred.resolve(Object)Deferred#reject(Object)
-
嵌套类概要
嵌套类 -
方法概要
修饰符和类型方法说明always(AlwaysCallback<? super D> callback) This method will registerAlwaysCallbackso that when a Deferred object is either resolved (Deferred.resolve(Object)) or rejected (Deferred#reject(Object)),AlwaysCallbackwill be triggered.This method will registerDoneCallbackso that when a Deferred object is resolved (Deferred.resolve(Object)),DoneCallbackwill be triggered.This method will registerFailCallbackso that when a Deferred object is rejected (Deferred#reject(Object)),FailCallbackwill be triggered.get the resolved value, return defaultValue for rejectedget the resolved value, return defaultSupplier‘s result for rejectedbooleanQueries the state of this promise, returningtrueiff it isState.PENDING.booleanQueries the state of this promise, returningtrueiff it isState.REJECTED.booleanQueries the state of this promise, returningtrueiff it isState.RESOLVED.state()状态Equivalent todone(DoneCallback)Equivalent todone(DoneCallback).fail(FailCallback)voidThis method will wait as long as the State is Pending.voidwaitSafely(long timeout) This method will wait when the State is Pending, and return when timeout has reached.
-
方法详细资料
-
state
Promise.State state()状态- 返回:
- the state of this promise.
-
isPending
boolean isPending()Queries the state of this promise, returningtrueiff it isState.PENDING.- 返回:
trueif the current state of this promise isState.PENDING,falseotherwise.- 另请参阅:
-
isResolved
boolean isResolved()Queries the state of this promise, returningtrueiff it isState.RESOLVED.- 返回:
trueif the current state of this promise isState.RESOLVED,falseotherwise.- 另请参阅:
-
isRejected
boolean isRejected()Queries the state of this promise, returningtrueiff it isState.REJECTED.- 返回:
trueif the current state of this promise isState.REJECTED,falseotherwise.- 另请参阅:
-
getOrElse
get the resolved value, return defaultValue for rejected- 参数:
defaultValue-- 返回:
-
getOrElse
get the resolved value, return defaultSupplier‘s result for rejected- 参数:
defaultSupplier-- 返回:
-
then
Equivalent todone(DoneCallback)- 参数:
doneCallback- seedone(DoneCallback)- 返回:
thisfor chaining more calls
-
then
Equivalent todone(DoneCallback).fail(FailCallback)- 参数:
doneCallback- seedone(DoneCallback)failCallback- seefail(FailCallback)- 返回:
thisfor chaining more calls
-
done
This method will registerDoneCallbackso that when a Deferred object is resolved (Deferred.resolve(Object)),DoneCallbackwill be triggered. If the Deferred object is already resolved then theDoneCallbackis triggered immediately. You can register multipleDoneCallbackby 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- 返回:
thisfor chaining more calls- 另请参阅:
-
fail
This method will registerFailCallbackso that when a Deferred object is rejected (Deferred#reject(Object)),FailCallbackwill be triggered. If the Deferred object is already rejected then theFailCallbackis triggered immediately. You can register multipleFailCallbackby 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- 返回:
thisfor chaining more calls- 另请参阅:
-
Deferred#reject(Object)
-
always
This method will registerAlwaysCallbackso that when a Deferred object is either resolved (Deferred.resolve(Object)) or rejected (Deferred#reject(Object)),AlwaysCallbackwill be triggered. If the Deferred object is already resolved or rejected then theAlwaysCallbackis triggered immediately. You can register multipleAlwaysCallbackby calling the method multiple times. The order of callback trigger is based on the order they have been registered.AlwaysCallbacks are triggered after anyDoneCallbackorFailCallbackrespectively.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- 返回:
thisfor chaining more calls- 另请参阅:
-
Deferred.resolve(Object)Deferred#reject(Object)
-
waitSafely
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
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
-