Interface Promise<D>
- Type Parameters:
D- 用于成功回调的数据类型
- All Superinterfaces:
Supplier<D>
- All Known Subinterfaces:
Deferred<D>
- All Known Implementing Classes:
AbstractPromise,DeferredObject
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 -
Method Summary
Modifier and TypeMethodDescriptionalways(AlwaysCallback<? super D> callback) This method will registerAlwaysCallbackso that when a Deferred object is either resolved (Deferred.resolve(Object)) or rejected (),invalid reference
Deferred#reject(Object)AlwaysCallbackwill be triggered.This method will registerso that when a Deferred object is resolved (invalid reference
DoneCallbackDeferred.resolve(Object)),will be triggered.invalid reference
DoneCallbackThis method will registerso that when a Deferred object is rejected (invalid reference
FailCallback),invalid reference
Deferred#reject(Object)will be triggered.invalid reference
FailCallbackget 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.
-
Method Details
-
state
Promise.State state()状态- Returns:
- the state of this promise.
-
isPending
boolean isPending()Queries the state of this promise, returningtrueiff it isState.PENDING.- Returns:
trueif the current state of this promise isState.PENDING,falseotherwise.- See Also:
-
isResolved
boolean isResolved()Queries the state of this promise, returningtrueiff it isState.RESOLVED.- Returns:
trueif the current state of this promise isState.RESOLVED,falseotherwise.- See Also:
-
isRejected
boolean isRejected()Queries the state of this promise, returningtrueiff it isState.REJECTED.- Returns:
trueif the current state of this promise isState.REJECTED,falseotherwise.- See Also:
-
getOrElse
get the resolved value, return defaultValue for rejected- Parameters:
defaultValue- 默认值- Returns:
- 如果Promise已解决则返回解决的值,否则返回默认值
-
getOrElse
get the resolved value, return defaultSupplier‘s result for rejected- Parameters:
defaultSupplier- 默认值提供者- Returns:
- 如果Promise已解决则返回解决的值,否则返回默认值
-
then
Equivalent todone(DoneCallback)- Parameters:
doneCallback- seedone(DoneCallback)- Returns:
thisfor chaining more calls
-
then
Equivalent todone(DoneCallback).fail(FailCallback)- Parameters:
doneCallback- seedone(DoneCallback)failCallback- seefail(FailCallback)- Returns:
thisfor chaining more calls
-
done
This method will registerso that when a Deferred object is resolved (invalid reference
DoneCallbackDeferred.resolve(Object)),will be triggered. If the Deferred object is already resolved then theinvalid reference
DoneCallbackis triggered immediately. You can register multipleinvalid reference
DoneCallbackby calling the method multiple times. The order of callback trigger is based on the order they have been registered.invalid reference
DoneCallbackpromise.progress(new DoneCallback(){ public void onDone(Object done) { ... } });- Parameters:
callback- the callback to be triggered- Returns:
thisfor chaining more calls- See Also:
-
fail
This method will registerso that when a Deferred object is rejected (invalid reference
FailCallback),invalid reference
Deferred#reject(Object)will be triggered. If the Deferred object is already rejected then theinvalid reference
FailCallbackis triggered immediately. You can register multipleinvalid reference
FailCallbackby calling the method multiple times. The order of callback trigger is based on the order they have been registered.invalid reference
FailCallbackpromise.fail(new FailCallback(){ public void onFail(Object rejection) { ... } });- Parameters:
callback- the callback to be triggered- Returns:
thisfor chaining more calls- See Also:
-
always
This method will registerAlwaysCallbackso that when a Deferred object is either resolved (Deferred.resolve(Object)) or rejected (),invalid reference
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 anyorinvalid reference
DoneCallbackrespectively.invalid reference
FailCallbackpromise.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:
thisfor chaining more calls- See Also:
-
waitSafely
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
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
-