Interface Promise<T>
- Type Parameters:
T
- The value type associated with this Promise.
A Promise represents a future value. It handles the interactions for
asynchronous processing. A Deferred
object can be used to create a
Promise and later resolve the Promise. A Promise is used by the caller of an
asynchronous function to get the result or handle the error. The caller can
either get a callback when the Promise is resolved with a value or an error,
or the Promise can be used in chaining. In chaining, callbacks are provided
that receive the resolved Promise, and a new Promise is generated that
resolves based upon the result of a callback.
Both callbacks
and
chaining
can be repeated any number of times,
even after the Promise has been resolved.
Example callback usage:
Promise<String> foo = foo(); foo.onResolve(() -> System.out.println("resolved"));Example chaining usage;
Success<String,String> doubler = p -> Promises .resolved(p.getValue() + p.getValue()); Promise<String> foo = foo().then(doubler).then(doubler);
- "ThreadSafe"
-
Method Summary
Modifier and TypeMethodDescriptiondelay
(long milliseconds) Delay after the resolution of this Promise.fallbackTo
(Promise<? extends T> fallback) Fall back to the value of the specified Promise if this Promise fails.fallbackTo
(Promise<? extends T> fallback, Class<?> failureType) Fall back to the value of the specified Promise if this Promise fails and the failure is an instance of a failure type.Filter the value of this Promise.<R> Promise<R>
FlatMap the value of this Promise.Returns the failure of this Promise.getValue()
Returns the value of this Promise.boolean
isDone()
Returns whether this Promise has been resolved.<R> Promise<R>
Map the value of this Promise.Register a callback to be called with the failure for this Promise when this Promise is resolved with a failure of a failure type.Register a callback to be called with the failure for this Promise when this Promise is resolved with a failure.Register a callback to be called when this Promise is resolved.Register a callback to be called with the result of this Promise when this Promise is resolved successfully.Recover from a failure of this Promise with a recovery value.Recover from a failure of this Promise with a recovery value if the failure is an instance of a failure type.recoverWith
(Function<Promise<?>, Promise<? extends T>> recovery) Recover from a failure of this Promise with a recovery Promise.Recover from a failure of this Promise with a recovery Promise if the failure is an instance of a failure type.<R> Promise<R>
Chain a new Promise to this Promise with a Success callback.<R> Promise<R>
Chain a new Promise to this Promise with Success and Failure callbacks.thenAccept
(Consumer<? super T> consumer) Chain a new Promise to this Promise with a Consumer callback that receives the value of this Promise when it is successfully resolved.timeout
(long milliseconds) Time out the resolution of this Promise.Returns a new CompletionStage that will be resolved with the result of this Promise.
-
Method Details
-
isDone
boolean isDone()Returns whether this Promise has been resolved.This Promise may be successfully resolved or resolved with a failure.
- Returns:
true
if this Promise was resolved either successfully or with a failure;false
if this Promise is unresolved.
-
getValue
Returns the value of this Promise.If this Promise is not
resolved
, this method must block and wait for this Promise to be resolved before completing.If this Promise was successfully resolved, this method returns with the value of this Promise. If this Promise was resolved with a failure, this method must throw an
InvocationTargetException
with thefailure exception
as the cause.- Returns:
- The value of this resolved Promise.
- Throws:
InvocationTargetException
- If this Promise was resolved with a failure. The cause of theInvocationTargetException
is the failure exception.InterruptedException
- If the current thread was interrupted while waiting.
-
getFailure
Returns the failure of this Promise.If this Promise is not
resolved
, this method must block and wait for this Promise to be resolved before completing.If this Promise was resolved with a failure, this method returns with the failure of this Promise. If this Promise was successfully resolved, this method must return
null
.- Returns:
- The failure of this resolved Promise or
null
if this Promise was successfully resolved. - Throws:
InterruptedException
- If the current thread was interrupted while waiting.
-
onResolve
Register a callback to be called when this Promise is resolved.The specified callback is called when this Promise is resolved either successfully or with a failure.
This method may be called at any time including before and after this Promise has been resolved.
Resolving this Promise happens-before any registered callback is called. That is, in a registered callback,
isDone()
must returntrue
andgetValue()
andgetFailure()
must not block.A callback may be called on a different thread than the thread which registered the callback. So the callback must be thread safe but can rely upon that the registration of the callback happens-before the registered callback is called.
- Parameters:
callback
- The callback to be called when this Promise is resolved. Must not benull
.- Returns:
- This Promise.
-
onSuccess
Register a callback to be called with the result of this Promise when this Promise is resolved successfully. The callback will not be called if this Promise is resolved with a failure.This method may be called at any time including before and after this Promise has been resolved.
Resolving this Promise happens-before any registered callback is called. That is, in a registered callback,
isDone()
must returntrue
andgetValue()
andgetFailure()
must not block.A callback may be called on a different thread than the thread which registered the callback. So the callback must be thread safe but can rely upon that the registration of the callback happens-before the registered callback is called.
- Parameters:
success
- The Consumer callback that receives the value of this Promise. Must not benull
.- Returns:
- This Promise.
- Since:
- 1.1
-
onFailure
Register a callback to be called with the failure for this Promise when this Promise is resolved with a failure. The callback will not be called if this Promise is resolved successfully.This method may be called at any time including before and after this Promise has been resolved.
Resolving this Promise happens-before any registered callback is called. That is, in a registered callback,
isDone()
must returntrue
andgetValue()
andgetFailure()
must not block.A callback may be called on a different thread than the thread which registered the callback. So the callback must be thread safe but can rely upon that the registration of the callback happens-before the registered callback is called.
- Parameters:
failure
- The Consumer callback that receives the failure of this Promise. Must not benull
.- Returns:
- This Promise.
- Since:
- 1.1
-
then
Chain a new Promise to this Promise with Success and Failure callbacks.The specified
Success
callback is called when this Promise is successfully resolved and the specifiedFailure
callback is called when this Promise is resolved with a failure.This method returns a new Promise which is chained to this Promise. The returned Promise must be resolved when this Promise is resolved after the specified Success or Failure callback is executed. The result of the executed callback must be used to resolve the returned Promise. Multiple calls to this method can be used to create a chain of promises which are resolved in sequence.
If this Promise is successfully resolved, the Success callback is executed and the result Promise, if any, or thrown exception is used to resolve the returned Promise from this method. If this Promise is resolved with a failure, the Failure callback is executed and the returned Promise from this method is failed.
This method may be called at any time including before and after this Promise has been resolved.
Resolving this Promise happens-before any registered callback is called. That is, in a registered callback,
isDone()
must returntrue
andgetValue()
andgetFailure()
must not block.A callback may be called on a different thread than the thread which registered the callback. So the callback must be thread safe but can rely upon that the registration of the callback happens-before the registered callback is called.
- Type Parameters:
R
- The value type associated with the returned Promise.- Parameters:
success
- The Success callback to be called when this Promise is successfully resolved. May benull
if no Success callback is required. In this case, the returned Promise must be resolved with the valuenull
when this Promise is successfully resolved.failure
- The Failure callback to be called when this Promise is resolved with a failure. May benull
if no Failure callback is required.- Returns:
- A new Promise which is chained to this Promise. The returned Promise must be resolved when this Promise is resolved after the specified Success or Failure callback, if any, is executed.
-
then
Chain a new Promise to this Promise with a Success callback.This method performs the same function as calling
then(Success, Failure)
with the specified Success callback andnull
for the Failure callback.- Type Parameters:
R
- The value type associated with the returned Promise.- Parameters:
success
- The Success callback to be called when this Promise is successfully resolved. May benull
if no Success callback is required. In this case, the returned Promise must be resolved with the valuenull
when this Promise is successfully resolved.- Returns:
- A new Promise which is chained to this Promise. The returned Promise must be resolved when this Promise is resolved after the specified Success, if any, is executed.
- See Also:
-
thenAccept
Chain a new Promise to this Promise with a Consumer callback that receives the value of this Promise when it is successfully resolved.The specified
Consumer
is called when this Promise is resolved successfully.This method returns a new Promise which is chained to this Promise. The returned Promise must be resolved when this Promise is resolved after the specified callback is executed. If the callback throws an exception, the returned Promise is failed with that exception. Otherwise the returned Promise is resolved with the success value from this Promise.
This method may be called at any time including before and after this Promise has been resolved.
Resolving this Promise happens-before any registered callback is called. That is, in a registered callback,
isDone()
must returntrue
andgetValue()
andgetFailure()
must not block.A callback may be called on a different thread than the thread which registered the callback. So the callback must be thread safe but can rely upon that the registration of the callback happens-before the registered callback is called.
- Parameters:
consumer
- The Consumer callback that receives the value of this Promise. Must not benull
.- Returns:
- A new Promise which is chained to this Promise. The returned Promise must be resolved when this Promise is resolved after the specified Consumer is executed.
- Since:
- 1.1
-
filter
Filter the value of this Promise.If this Promise is successfully resolved, the returned Promise must either be resolved with the value of this Promise, if the specified Predicate accepts that value, or failed with a
NoSuchElementException
, if the specified Predicate does not accept that value. If the specified Predicate throws an exception, the returned Promise must be failed with the exception.If this Promise is resolved with a failure, the returned Promise must be failed with that failure.
This method may be called at any time including before and after this Promise has been resolved.
- Parameters:
predicate
- The Predicate to evaluate the value of this Promise. Must not benull
.- Returns:
- A Promise that filters the value of this Promise.
-
map
Map the value of this Promise.If this Promise is successfully resolved, the returned Promise must be resolved with the value of specified Function as applied to the value of this Promise. If the specified Function throws an exception, the returned Promise must be failed with the exception.
If this Promise is resolved with a failure, the returned Promise must be failed with that failure.
This method may be called at any time including before and after this Promise has been resolved.
- Type Parameters:
R
- The value type associated with the returned Promise.- Parameters:
mapper
- The Function that must map the value of this Promise to the value that must be used to resolve the returned Promise. Must not benull
.- Returns:
- A Promise that returns the value of this Promise as mapped by the specified Function.
-
flatMap
FlatMap the value of this Promise.If this Promise is successfully resolved, the returned Promise must be resolved with the Promise from the specified Function as applied to the value of this Promise. If the specified Function throws an exception, the returned Promise must be failed with the exception.
If this Promise is resolved with a failure, the returned Promise must be failed with that failure.
This method may be called at any time including before and after this Promise has been resolved.
- Type Parameters:
R
- The value type associated with the returned Promise.- Parameters:
mapper
- The Function that must flatMap the value of this Promise to a Promise that must be used to resolve the returned Promise. Must not benull
.- Returns:
- A Promise that returns the value of this Promise as mapped by the specified Function.
-
recover
Recover from a failure of this Promise with a recovery value.If this Promise is successfully resolved, the returned Promise must be resolved with the value of this Promise.
If this Promise is resolved with a failure, the specified Function is applied to this Promise to produce a recovery value.
- If the recovery value is not
null
, the returned Promise must be resolved with the recovery value. - If the recovery value is
null
, the returned Promise must be failed with the failure of this Promise. - If the specified Function throws an exception, the returned Promise must be failed with that exception.
To recover from a failure of this Promise with a recovery value of
null
, therecoverWith(Function)
method must be used. The specified Function forrecoverWith(Function)
can returnPromises.resolved(null)
to supply the desirednull
value.This method may be called at any time including before and after this Promise has been resolved.
- Parameters:
recovery
- If this Promise resolves with a failure, the specified Function is called to produce a recovery value to be used to resolve the returned Promise. Must not benull
.- Returns:
- A Promise that resolves with the value of this Promise or recovers from the failure of this Promise.
- If the recovery value is not
-
recoverWith
Recover from a failure of this Promise with a recovery Promise.If this Promise is successfully resolved, the returned Promise must be resolved with the value of this Promise.
If this Promise is resolved with a failure, the specified Function is applied to this Promise to produce a recovery Promise.
- If the recovery Promise is not
null
, the returned Promise must be resolved with the recovery Promise. - If the recovery Promise is
null
, the returned Promise must be failed with the failure of this Promise. - If the specified Function throws an exception, the returned Promise must be failed with that exception.
This method may be called at any time including before and after this Promise has been resolved.
- Parameters:
recovery
- If this Promise resolves with a failure, the specified Function is called to produce a recovery Promise to be used to resolve the returned Promise. Must not benull
.- Returns:
- A Promise that resolves with the value of this Promise or recovers from the failure of this Promise.
- If the recovery Promise is not
-
fallbackTo
Fall back to the value of the specified Promise if this Promise fails.If this Promise is successfully resolved, the returned Promise must be resolved with the value of this Promise.
If this Promise is resolved with a failure, the successful result of the specified Promise is used to resolve the returned Promise. If the specified Promise is resolved with a failure, the returned Promise must be failed with the failure of this Promise rather than the failure of the specified Promise.
This method may be called at any time including before and after this Promise has been resolved.
- Parameters:
fallback
- The Promise whose value must be used to resolve the returned Promise if this Promise resolves with a failure. Must not benull
.- Returns:
- A Promise that returns the value of this Promise or falls back to the value of the specified Promise.
-
timeout
Time out the resolution of this Promise.If this Promise is successfully resolved before the timeout, the returned Promise is resolved with the value of this Promise. If this Promise is resolved with a failure before the timeout, the returned Promise is resolved with the failure of this Promise. If the timeout is reached before this Promise is resolved, the returned Promise is failed with a
TimeoutException
.- Parameters:
milliseconds
- The time to wait in milliseconds. Zero and negative time is treated as an immediate timeout.- Returns:
- A Promise that is resolved when either this Promise is resolved or the specified timeout is reached.
- Since:
- 1.1
-
delay
Delay after the resolution of this Promise.Once this Promise is resolved, resolve the returned Promise with this Promise after the specified delay.
- Parameters:
milliseconds
- The time to delay in milliseconds. Zero and negative time is treated as no delay.- Returns:
- A Promise that is resolved with this Promise after this Promise is resolved and the specified delay has elapsed.
- Since:
- 1.1
-
toCompletionStage
CompletionStage<T> toCompletionStage()Returns a new CompletionStage that will be resolved with the result of this Promise.- Returns:
- A new CompletionStage that will be resolved with the result of this Promise.
- Since:
- 1.2
-
onFailure
Register a callback to be called with the failure for this Promise when this Promise is resolved with a failure of a failure type. The callback will not be called if this Promise is resolved successfully or if the failure is not an instance of the specified failure type.This method may be called at any time including before and after this Promise has been resolved.
Resolving this Promise happens-before any registered callback is called. That is, in a registered callback,
isDone()
must returntrue
andgetValue()
andgetFailure()
must not block.A callback may be called on a different thread than the thread which registered the callback. So the callback must be thread safe but can rely upon that the registration of the callback happens-before the registered callback is called.
- Type Parameters:
F
- The failure type.- Parameters:
failure
- The Consumer callback that receives the failure of this Promise if the failure is an instance of the specified failure type. Must not benull
.failureType
- The type of failure for which this callback will be called. If the failure is not an instance of the specified failure type, the specified callback will not be called. Must not benull
.- Returns:
- This Promise.
- Since:
- 1.3
-
recover
Recover from a failure of this Promise with a recovery value if the failure is an instance of a failure type.If this Promise is successfully resolved, the returned Promise must be resolved with the value of this Promise.
If this Promise is resolved with a failure and the failure is not an instance of the specified failure type, the returned Promise must be failed with the failure of this Promise.
If this Promise is resolved with a failure and the failure is an instance of the specified failure type, the specified Function is applied to this Promise to produce a recovery value.
- If the recovery value is not
null
, the returned Promise must be resolved with the recovery value. - If the recovery value is
null
, the returned Promise must be failed with the failure of this Promise. - If the specified Function throws an exception, the returned Promise must be failed with that exception.
To recover from a failure of this Promise with a recovery value of
null
, therecoverWith(Function, Class)
method must be used. The specified Function forrecoverWith(Function, Class)
can returnPromises.resolved(null)
to supply the desirednull
value.This method may be called at any time including before and after this Promise has been resolved.
- Parameters:
recovery
- If this Promise resolves with a failure and the failure is an instance of the specified failure type, the specified Function is called to produce a recovery value to be used to resolve the returned Promise. Must not benull
.failureType
- The type of failure for which the specified recovery will be used. If the failure is not an instance of the failure type, the specified recovery will not be called and the returned Promise must be failed with the failure of this Promise. Must not benull
.- Returns:
- A Promise that resolves with the value of this Promise or recovers from the failure of this Promise if the failure is an instance of the specified failure type.
- Since:
- 1.3
- If the recovery value is not
-
recoverWith
Recover from a failure of this Promise with a recovery Promise if the failure is an instance of a failure type.If this Promise is successfully resolved, the returned Promise must be resolved with the value of this Promise.
If this Promise is resolved with a failure and the failure is not an instance of the specified failure type, the returned Promise must be failed with the failure of this Promise.
If this Promise is resolved with a failure and the failure is an instance of the specified failure type, the specified Function is applied to this Promise to produce a recovery Promise.
- If the recovery Promise is not
null
, the returned Promise must be resolved with the recovery Promise. - If the recovery Promise is
null
, the returned Promise must be failed with the failure of this Promise. - If the specified Function throws an exception, the returned Promise must be failed with that exception.
This method may be called at any time including before and after this Promise has been resolved.
- Parameters:
recovery
- If this Promise resolves with a failure and the failure is an instance of the specified failure type, the specified Function is called to produce a recovery Promise to be used to resolve the returned Promise. Must not benull
.failureType
- The type of failure for which this recovery will be used. If the failure is not an instance of the failure type, the specified recovery will not be called and the returned Promise must be failed with the failure of this Promise. Must not benull
.- Returns:
- A Promise that resolves with the value of this Promise or recovers from the failure of this Promise if the failure is an instance of the specified failure type.
- Since:
- 1.3
- If the recovery Promise is not
-
fallbackTo
Fall back to the value of the specified Promise if this Promise fails and the failure is an instance of a failure type.If this Promise is successfully resolved, the returned Promise must be resolved with the value of this Promise.
If this Promise is resolved with a failure and the failure is an instance of the specified failure type, the successful result of the specified Promise is used to resolve the returned Promise. If the specified Promise is resolved with a failure, the returned Promise must be failed with the failure of this Promise rather than the failure of the specified Promise.
If this Promise is resolved with a failure and the failure is not an instance of the specified failure type, the returned Promise must be failed with the failure of this Promise.
This method may be called at any time including before and after this Promise has been resolved.
- Parameters:
fallback
- The Promise whose value must be used to resolve the returned Promise if this Promise resolves with a failure and the failure is an instance of the specified failure type. Must not benull
.failureType
- The type of failure for which this recovery will be used. If the failure is not an instance of the failure type, the specified recovery will not be called and the returned Promise must be failed with the failure of this Promise. Must not benull
.- Returns:
- A Promise that returns the value of this Promise or falls back to the value of the specified Promise if the failure is an instance of the specified failure type.
- Since:
- 1.3
-