public interface Promise<V>
Future
with the following differences:
Future
and its derivatives to implement activities and workflow starting and querying code.
get()
doesn't throw InterruptedException. The only way to unblock get()
is to complete the Promise
CompletablePromise.completeExceptionally(RuntimeException)
are
not wrapped. It is possible as CompletablePromise.completeExceptionally(RuntimeException)
accepts only runtime
exceptions. So wrapping must be done by the caller of that method.
CancellationScope
to cancel and
handle cancellations. The pattern is that a cancelled operation completes its Promise with
CancellationException
when cancelled.
handle(Functions.Func2)
and similar callback operations do not allow blocking
calls inside functions
Modifier and Type | Method and Description |
---|---|
static <U> Promise<java.util.List<U>> |
allOf(java.util.Collection<Promise<U>> promises)
Returns Promise that becomes completed when all promises in the collection are completed.
|
static Promise<java.lang.Void> |
allOf(Promise<?>... promises)
Returns Promise that becomes completed when all arguments are completed.
|
static Promise<java.lang.Object> |
anyOf(java.lang.Iterable<Promise<?>> promises)
Returns Promise that becomes completed when any of the arguments is completed.
|
static Promise<java.lang.Object> |
anyOf(Promise<?>... promises)
Returns Promise that becomes completed when any of the arguments is completed.
|
Promise<V> |
exceptionally(Functions.Func1<java.lang.Throwable,? extends V> fn)
Returns a new Promise that, when this promise completes exceptionally, is executed with this
promise's exception as the argument to the supplied function.
|
V |
get()
Waits if necessary for the computation to complete or fail, and then returns its result.
|
V |
get(long timeout,
java.util.concurrent.TimeUnit unit)
Waits if necessary for at most the given time for the computation to complete, and then returns
its result, if available.
|
V |
get(long timeout,
java.util.concurrent.TimeUnit unit,
V defaultValue)
Waits if necessary for at most the given time for the computation to complete, and then
retrieves its result, if available.
|
V |
get(V defaultValue)
Waits if necessary for the computation to complete or fail, and then returns its result or
defaultValue in case of failure.
|
java.lang.RuntimeException |
getFailure()
Waits if necessary for the computation to complete or fail, and then returns the failure or
null.
|
<U> Promise<U> |
handle(Functions.Func2<? super V,java.lang.RuntimeException,? extends U> fn)
Returns Promise that contains a result of a function.
|
boolean |
isCompleted()
Returns
true if this promise is completed. |
<U> Promise<U> |
thenApply(Functions.Func1<? super V,? extends U> fn)
Returns Promise that contains a result of a function.
|
<U> Promise<U> |
thenCompose(Functions.Func1<? super V,? extends Promise<U>> fn)
Returns a new Promise that, when this promise completes normally, is executed with this promise
as the argument to the supplied function.
|
boolean isCompleted()
true
if this promise is completed.
Completion may be due to normal termination, an exception, or cancellation -- in all of
these cases, this method will return true
.
true
if this promise completedV get()
java.lang.RuntimeException
- if the computation failed.V get(V defaultValue)
defaultValue
- value to return in case of failurejava.lang.RuntimeException
- if the computation failed.V get(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutException
timeout
- the maximum time to waitunit
- the time unit of the timeout argumentjava.lang.RuntimeException
- if the computation failed.java.util.concurrent.TimeoutException
- if the wait timed outV get(long timeout, java.util.concurrent.TimeUnit unit, V defaultValue)
timeout
- the maximum time to waitunit
- the time unit of the timeout argumentdefaultValue
- value to return in case of timeout or failurejava.lang.RuntimeException getFailure()
<U> Promise<U> thenApply(Functions.Func1<? super V,? extends U> fn)
Note that no blocking calls are allowed inside of the function.
<U> Promise<U> handle(Functions.Func2<? super V,java.lang.RuntimeException,? extends U> fn)
RuntimeException
it fails the resulting promise.
Note that no blocking calls are allowed inside of the function.
<U> Promise<U> thenCompose(Functions.Func1<? super V,? extends Promise<U>> fn)
U
- the type of the returned CompletionStage's resultfn
- the function returning a new PromisePromise<V> exceptionally(Functions.Func1<java.lang.Throwable,? extends V> fn)
fn
- the function to use to compute the value of the returned CompletionPromise if this
CompletionPromise completed exceptionallystatic <U> Promise<java.util.List<U>> allOf(java.util.Collection<Promise<U>> promises)
promises
- promises to wait for.static Promise<java.lang.Void> allOf(Promise<?>... promises)
static Promise<java.lang.Object> anyOf(java.lang.Iterable<Promise<?>> promises)