Class PromiseImpl<V,E extends Exception>
- Type Parameters:
V
- The type of the task's result, orVoid
if the task does not return anything (i.e. it only has side-effects).E
- The type of the exception thrown by the task if it fails, orNeverThrowsException
if the task cannot fail.
- All Implemented Interfaces:
ExceptionHandler<E>
,Promise<V,
,E> ResultHandler<V>
,RuntimeExceptionHandler
Promise
which can be used as is, or as the basis
for more complex asynchronous behavior. A PromiseImpl
must be
completed by invoking one of:
handleResult(V)
- marks the promise as having succeeded with the provide resulthandleException(E)
- marks the promise as having failed with the provided exceptioncancel(boolean)
- requests cancellation of the asynchronous task represented by the promise. Cancellation is only supported if thetryCancel(boolean)
is overridden and returns an exception.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal boolean
cancel
(boolean mayInterruptIfRunning) Attempts to cancel the asynchronous task associated with thisPromise
.static <V,
E extends Exception>
PromiseImpl<V,E> create()
Creates a new pendingPromise
implementation.final V
get()
Waits if necessary for thisPromise
to complete, and then returns the result if it completed successfully, or throws anExecutionException
containing the cause of the failure.final V
Waits if necessary for at most the given time for thisPromise
to complete, and then returns the result if it completed successfully, or throws anExecutionException
containing the cause of the failure.final V
Waits if necessary for thisPromise
to complete, and then returns the result if it completed successfully, or throws an exception representing the cause of the failure.final V
getOrThrow
(long timeout, TimeUnit unit) Waits if necessary for at most the given time for thisPromise
to complete, and then returns the result if it completed successfully, or throws an exception representing the cause of the failure.final V
Deprecated.final V
getOrThrowUninterruptibly
(long timeout, TimeUnit unit) Deprecated.final void
handleException
(E exception) Signals that the asynchronous task represented by this promise has failed.final void
handleResult
(V result) Signals that the asynchronous task represented by this promise has succeeded.void
handleRuntimeException
(RuntimeException exception) Invoked when the asynchronous task has failed with a runtime exception.final boolean
Returnstrue
if thisPromise
was cancelled before it completed normally.final boolean
isDone()
Returnstrue
if thisPromise
has completed.final boolean
isResult()
Returnstrue
if thisPromise
has completed and contains a value.Submits the provided function for execution once thisPromise
has completed with a result, and returns a newPromise
representing the outcome of the function.Submits the provided functions for execution once thisPromise
has completed (with a result or an exception), and returns a newPromise
representing the outcome of the invoked function.then
(Function<? super V, VOUT, EOUT> onResult, Function<? super E, VOUT, EOUT> onException, Function<? super RuntimeException, VOUT, EOUT> onRuntimeException) Submits the provided functions for execution once thisPromise
has completed (with a result or an exception or aRuntimeException
), and returns a newPromise
representing the outcome of the invoked function.thenAlways
(Runnable always) Submits the provided runnable for execution once thisPromise
has completed, and regardless of whether it has a result or an exception.thenAsync
(AsyncFunction<? super V, VOUT, E> onResult) Submits the provided asynchronous function for execution once thisPromise
has completed with a result, and returns a newPromise
representing the outcome of the function.thenAsync
(AsyncFunction<? super V, VOUT, EOUT> onResult, AsyncFunction<? super E, VOUT, EOUT> onException) Submits the provided asynchronous functions for execution once thisPromise
has completed, and returns a newPromise
representing the outcome of the invoked function.thenAsync
(AsyncFunction<? super V, VOUT, EOUT> onResult, AsyncFunction<? super E, VOUT, EOUT> onException, AsyncFunction<? super RuntimeException, VOUT, EOUT> onRuntimeException) Submits the provided asynchronous functions for execution once thisPromise
has completed, and returns a newPromise
representing the outcome of the invoked function.Submits the provided function for execution once thisPromise
has not completed with a result (has completed with an exception), and returns a newPromise
representing the outcome of the function.thenCatchAsync
(AsyncFunction<? super E, V, EOUT> onException) Submits the provided asynchronous function for execution once thisPromise
has completed with an exception, and returns a newPromise
representing the outcome of the function.thenCatchRuntimeException
(Function<? super RuntimeException, V, E> onRuntimeException) Submits the provided function for execution once thisPromise
has not completed with a result nor with an exception but with aRuntimeException
, and returns a newPromise
representing the outcome of the function.thenCatchRuntimeExceptionAsync
(AsyncFunction<? super RuntimeException, V, E> onRuntimeException) Submits the provided asynchronous function for execution once thisPromise
has completed with aRuntimeException
, and returns a newPromise
representing the outcome of the function.thenFinally
(Runnable onFinally) Submits the provided runnable for execution once thisPromise
has completed, and regardless of whether of its outcome.final <T extends ResultHandler<? super V> & ExceptionHandler<? super E> & RuntimeExceptionHandler>
Promise<V,E> thenOnCompletion
(T handler) Registers the provided completion handler for notification for all completion cases of thisPromise
.thenOnException
(ExceptionHandler<? super E> onException) Registers the provided completion handler for notification if thisPromise
cannot be completed due to an exception.thenOnResult
(ResultHandler<? super V> onResult) Registers the provided completion handler for notification once thisPromise
has completed with a result.thenOnResultOrException
(Runnable onResultOrException) Submits the provided runnable for execution once thisPromise
has completed, and regardless of whether it has a result or an exception.thenOnResultOrException
(ResultHandler<? super V> onResult, ExceptionHandler<? super E> onException) Registers the provided completion handlers for notification once thisPromise
has completed (with a result or an exception).thenOnRuntimeException
(RuntimeExceptionHandler onRuntimeException) Registers the provided completion handler for notification if thisPromise
cannot be completed due to an runtime exception.protected E
tryCancel
(boolean mayInterruptIfRunning) Invoked when the client attempts to cancel the asynchronous task represented by this promise.final boolean
tryHandleException
(E exception) Attempts to signal that the asynchronous task represented by this promise has failed.final boolean
tryHandleResult
(V result) Attempts to signal that the asynchronous task represented by this promise has succeeded.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.forgerock.util.promise.Promise
getOrThrowIfInterrupted, thenDiscardResult
-
Constructor Details
-
PromiseImpl
protected PromiseImpl()Creates a new pendingPromise
implementation. This constructor is protected to allow for sub-classing.
-
-
Method Details
-
create
Creates a new pendingPromise
implementation.- Type Parameters:
V
- The type of the task's result, orVoid
if the task does not return anything (i.e. it only has side-effects).E
- The type of the exception thrown by the task if it fails, orNeverThrowsException
if the task cannot fail.- Returns:
- A new pending
Promise
implementation.
-
cancel
public final boolean cancel(boolean mayInterruptIfRunning) Description copied from interface:Promise
Attempts to cancel the asynchronous task associated with thisPromise
. Cancellation will fail if thisPromise
has already completed or has already been cancelled. If successful, then cancellation will complete thisPromise
with an appropriate exception and notify any registered functions and completion handlers.After this method returns, subsequent calls to
Promise.isDone()
will always returntrue
. Subsequent calls toPromise.isCancelled()
will always returntrue
if this method returnedtrue
.- Specified by:
cancel
in interfacePromise<V,
E extends Exception> - Parameters:
mayInterruptIfRunning
-true
if the thread executing executing the response handler should be interrupted; otherwise, in-progress response handlers are allowed to complete.- Returns:
false
ifPromise
could not be cancelled, typically because it has already completed normally;true
otherwise.
-
get
Description copied from interface:Promise
Waits if necessary for thisPromise
to complete, and then returns the result if it completed successfully, or throws anExecutionException
containing the cause of the failure.- Specified by:
get
in interfacePromise<V,
E extends Exception> - Returns:
- The result, but only if this
Promise
completed successfully. - Throws:
InterruptedException
- If the current thread was interrupted while waiting.ExecutionException
- If thisPromise
was cancelled or did not complete successfully. TheExecutionException
will contain the cause of the failure.
-
get
public final V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException Description copied from interface:Promise
Waits if necessary for at most the given time for thisPromise
to complete, and then returns the result if it completed successfully, or throws anExecutionException
containing the cause of the failure.- Specified by:
get
in interfacePromise<V,
E extends Exception> - Parameters:
timeout
- The maximum time to wait.unit
- The time unit of the timeout argument.- Returns:
- The result, but only if this
Promise
completed successfully. - Throws:
InterruptedException
- If the current thread was interrupted while waiting.ExecutionException
- If thisPromise
was cancelled or did not complete successfully. TheExecutionException
will contain the cause of the failure.TimeoutException
- If the wait timed out.
-
getOrThrow
Description copied from interface:Promise
Waits if necessary for thisPromise
to complete, and then returns the result if it completed successfully, or throws an exception representing the cause of the failure.- Specified by:
getOrThrow
in interfacePromise<V,
E extends Exception> - Returns:
- The result, but only if this
Promise
completed successfully. - Throws:
InterruptedException
- If the current thread was interrupted while waiting.E
- If thisPromise
was cancelled or did not complete successfully.
-
getOrThrow
public final V getOrThrow(long timeout, TimeUnit unit) throws InterruptedException, E, TimeoutException Description copied from interface:Promise
Waits if necessary for at most the given time for thisPromise
to complete, and then returns the result if it completed successfully, or throws an exception representing the cause of the failure.- Specified by:
getOrThrow
in interfacePromise<V,
E extends Exception> - Parameters:
timeout
- The maximum time to wait.unit
- The time unit of the timeout argument.- Returns:
- The result, but only if this
Promise
completed successfully. - Throws:
InterruptedException
- If the current thread was interrupted while waiting.E
- If thisPromise
was cancelled or did not complete successfully.TimeoutException
- If the wait timed out.
-
getOrThrowUninterruptibly
Deprecated.Description copied from interface:Promise
Waits if necessary for thisPromise
to complete, and then returns the result if it completed successfully, or throws an exception representing the cause of the failure.This method is similar to
Promise.getOrThrow()
except that it will ignore thread interrupts. When this method returns the status of the current thread will be interrupted if an interrupt was received while waiting. -
getOrThrowUninterruptibly
@Deprecated public final V getOrThrowUninterruptibly(long timeout, TimeUnit unit) throws E, TimeoutException Deprecated.Description copied from interface:Promise
Waits if necessary for at most the given time for thisPromise
to complete, and then returns the result if it completed successfully, or throws an exception representing the cause of the failure.This method is similar to
Promise.getOrThrow(long, TimeUnit)
except that it will ignore thread interrupts. When this method returns the status of the current thread will be interrupted if an interrupt was received while waiting.- Specified by:
getOrThrowUninterruptibly
in interfacePromise<V,
E extends Exception> - Parameters:
timeout
- The maximum time to wait.unit
- The time unit of the timeout argument.- Returns:
- The result, but only if this
Promise
completed successfully. - Throws:
E
- If thisPromise
was cancelled or did not complete successfully.TimeoutException
- If the wait timed out.
-
handleException
Signals that the asynchronous task represented by this promise has failed. If the task has already completed (i.e.isDone() == true
) then calling this method has no effect and the provided result will be discarded.- Specified by:
handleException
in interfaceExceptionHandler<V>
- Parameters:
exception
- The exception indicating why the task failed.- See Also:
-
handleRuntimeException
Description copied from interface:RuntimeExceptionHandler
Invoked when the asynchronous task has failed with a runtime exception.- Specified by:
handleRuntimeException
in interfaceRuntimeExceptionHandler
- Parameters:
exception
- The runtime exception indicating why the asynchronous task has failed.
-
handleResult
Signals that the asynchronous task represented by this promise has succeeded. If the task has already completed (i.e.isDone() == true
) then calling this method has no effect and the provided result will be discarded.- Specified by:
handleResult
in interfaceResultHandler<V>
- Parameters:
result
- The result of the asynchronous task (may benull
).- See Also:
-
tryHandleException
Attempts to signal that the asynchronous task represented by this promise has failed. If the task has already completed (i.e.isDone() == true
) then calling this method has no effect andfalse
is returned.This method should be used in cases where multiple threads may concurrently attempt to complete a promise and need to release resources if the completion attempt fails. For example, an asynchronous TCP connect attempt may complete after a timeout has expired. In this case the connection should be immediately closed because it is never going to be used.
- Parameters:
exception
- The exception indicating why the task failed.- Returns:
false
if this promise has already been completed, either due to normal termination, an exception, or cancellation (i.e.isDone() == true
).- See Also:
-
tryHandleResult
Attempts to signal that the asynchronous task represented by this promise has succeeded. If the task has already completed (i.e.isDone() == true
) then calling this method has no effect andfalse
is returned.This method should be used in cases where multiple threads may concurrently attempt to complete a promise and need to release resources if the completion attempt fails. For example, an asynchronous TCP connect attempt may complete after a timeout has expired. In this case the connection should be immediately closed because it is never going to be used.
- Parameters:
result
- The result of the asynchronous task (may benull
).- Returns:
false
if this promise has already been completed, either due to normal termination, an exception, or cancellation (i.e.isDone() == true
).- See Also:
-
isCancelled
public final boolean isCancelled()Description copied from interface:Promise
Returnstrue
if thisPromise
was cancelled before it completed normally.- Specified by:
isCancelled
in interfacePromise<V,
E extends Exception> - Returns:
true
if thisPromise
was cancelled before it completed normally, otherwisefalse
.
-
isDone
public final boolean isDone()Description copied from interface:Promise
Returnstrue
if thisPromise
has completed.Completion may be due to normal termination, an exception, or cancellation. In all of these cases, this method will return
true
. -
isResult
public final boolean isResult()Description copied from interface:Promise
Returnstrue
if thisPromise
has completed and contains a value. -
thenOnException
Description copied from interface:Promise
Registers the provided completion handler for notification if thisPromise
cannot be completed due to an exception. If thisPromise
completes with a result then the completion handler will not be notified.This method can be used for asynchronous completion notification.
- Specified by:
thenOnException
in interfacePromise<V,
E extends Exception> - Parameters:
onException
- The completion handler which will be notified upon failure completion of thisPromise
.- Returns:
- A
Promise
that is guaranteed to be completed once the provided callback has been executed.
-
thenOnResult
Description copied from interface:Promise
Registers the provided completion handler for notification once thisPromise
has completed with a result. If thisPromise
completes with an exception then the completion handler will not be notified.This method can be used for asynchronous completion notification and is equivalent to
Promise.then(Function)
.- Specified by:
thenOnResult
in interfacePromise<V,
E extends Exception> - Parameters:
onResult
- The completion handler which will be notified upon successful completion of thisPromise
.- Returns:
- A
Promise
that is guaranteed to be completed once the provided callback has been executed.
-
thenOnResultOrException
public final Promise<V,E> thenOnResultOrException(ResultHandler<? super V> onResult, ExceptionHandler<? super E> onException) Description copied from interface:Promise
Registers the provided completion handlers for notification once thisPromise
has completed (with a result or an exception). If thisPromise
completes with a result thenonResult
will be notified with the result, otherwiseonException
will be notified with the exception that occurred.This method can be used for asynchronous completion notification.
- Specified by:
thenOnResultOrException
in interfacePromise<V,
E extends Exception> - Parameters:
onResult
- The completion handler which will be notified upon completion with a result of thisPromise
.onException
- The completion handler which will be notified upon failure of thisPromise
.- Returns:
- A
Promise
that is guaranted to be completed once the provided callback has been executed.
-
thenOnResultOrException
Description copied from interface:Promise
Submits the provided runnable for execution once thisPromise
has completed, and regardless of whether it has a result or an exception.This method can be used for resource cleanup after a series of asynchronous tasks have completed. More specifically, this method should be used in a similar manner to
finally
statements intry...catch
expressions.This method is equivalent to
Promise.thenAlways(Runnable)
.- Specified by:
thenOnResultOrException
in interfacePromise<V,
E extends Exception> - Parameters:
onResultOrException
- The runnable which will be notified regardless of the final outcome of thisPromise
.- Returns:
- A
Promise
that is guaranteed to be completed once the provided callback has been executed.
-
then
Description copied from interface:Promise
Submits the provided function for execution once thisPromise
has completed with a result, and returns a newPromise
representing the outcome of the function. If thisPromise
does not complete with a result then the function will not be invoked and the exception will be forwarded to the returnedPromise
.This method can be used for transforming the result of an asynchronous task.
- Specified by:
then
in interfacePromise<V,
E extends Exception> - Type Parameters:
VOUT
- The type of the function's result, orVoid
if the function does not return anything (i.e. it only has side-effects). Note that the type may be different to the type of thisPromise
.- Parameters:
onResult
- The function which will be executed upon successful completion of thisPromise
.- Returns:
- A new
Promise
representing the outcome of the function.
-
thenCatch
Description copied from interface:Promise
Submits the provided function for execution once thisPromise
has not completed with a result (has completed with an exception), and returns a newPromise
representing the outcome of the function. If thisPromise
completes with a result then the function will not be invoked and the result notification will be forwarded to the returnedPromise
.This method can be used for transforming the result of an asynchronous task.
- Specified by:
thenCatch
in interfacePromise<V,
E extends Exception> - Type Parameters:
EOUT
- The type of the exception thrown by the function if it fails, orNeverThrowsException
if it cannot fails. Note that the type may be different to the type of thisPromise
.- Parameters:
onException
- The function which will be executed upon failure completion of thisPromise
.- Returns:
- A new
Promise
representing the outcome of the function.
-
thenCatchRuntimeException
public Promise<V,E> thenCatchRuntimeException(Function<? super RuntimeException, V, E> onRuntimeException) Description copied from interface:Promise
Submits the provided function for execution once thisPromise
has not completed with a result nor with an exception but with aRuntimeException
, and returns a newPromise
representing the outcome of the function. If thisPromise
completes with a result or an exception then the function will not be invoked and the result notification will be forwarded to the returnedPromise
.This method can be used for transforming the result of an asynchronous task.
- Specified by:
thenCatchRuntimeException
in interfacePromise<V,
E extends Exception> - Parameters:
onRuntimeException
- The function which will be executed upon failure completion of thisPromise
.- Returns:
- A new
Promise
representing the outcome of the function.
-
then
public final <VOUT,EOUT extends Exception> Promise<VOUT,EOUT> then(Function<? super V, VOUT, EOUT> onResult, Function<? super E, VOUT, EOUT> onException) Description copied from interface:Promise
Submits the provided functions for execution once thisPromise
has completed (with a result or an exception), and returns a newPromise
representing the outcome of the invoked function. If thisPromise
completes with a result thenonResult
will be invoked with the result, otherwiseonException
will be invoked with the exception that occurred.This method can be used for transforming the outcome of an asynchronous task.
- Specified by:
then
in interfacePromise<V,
E extends Exception> - Type Parameters:
VOUT
- The type of the functions' result, orVoid
if the functions do not return anything (i.e. they only have side-effects). Note that the type may be different to the type of thisPromise
.EOUT
- The type of the exception thrown by the functions if they fail, orNeverThrowsException
if they cannot fail. Note that the type may be different to the type of thisPromise
.- Parameters:
onResult
- The function which will be executed upon successful completion of thisPromise
.onException
- The function which will be executed upon failure of thisPromise
.- Returns:
- A new
Promise
representing the outcome of the invoked function.
-
then
public final <VOUT,EOUT extends Exception> Promise<VOUT,EOUT> then(Function<? super V, VOUT, EOUT> onResult, Function<? super E, VOUT, EOUT> onException, Function<? super RuntimeException, VOUT, EOUT> onRuntimeException) Description copied from interface:Promise
Submits the provided functions for execution once thisPromise
has completed (with a result or an exception or aRuntimeException
), and returns a newPromise
representing the outcome of the invoked function. If thisPromise
completes with a result thenonResult
will be invoked with the result, with aRuntimeException
thenonRuntimeException
will be invoked with the runtime exception that occurred, otherwiseonException
will be invoked with the exception that occurred.This method can be used for transforming the outcome of an asynchronous task.
- Specified by:
then
in interfacePromise<V,
E extends Exception> - Type Parameters:
VOUT
- The type of the functions' result, orVoid
if the functions do not return anything (i.e. they only have side-effects). Note that the type may be different to the type of thisPromise
.EOUT
- The type of the exception thrown by the functions if they fail, orNeverThrowsException
if they cannot fail. Note that the type may be different to the type of thisPromise
.- Parameters:
onResult
- The function which will be executed upon successful completion of thisPromise
.onException
- The function which will be executed upon failure of thisPromise
.onRuntimeException
- The function which will be executed upon failure withRuntimeException
of thisPromise
.- Returns:
- A new
Promise
representing the outcome of the invoked function.
-
thenAlways
Description copied from interface:Promise
Submits the provided runnable for execution once thisPromise
has completed, and regardless of whether it has a result or an exception.This method can be used for resource cleanup after a series of asynchronous tasks have completed. More specifically, this method should be used in a similar manner to
finally
statements intry...catch
expressions.This method is equivalent to
Promise.thenOnResultOrException(Runnable)
.- Specified by:
thenAlways
in interfacePromise<V,
E extends Exception> - Parameters:
always
- The runnable which will be notified regardless of the final outcome of thisPromise
.- Returns:
- A
Promise
that is guaranteed to be completed once the provided callback has been executed.
-
thenFinally
Description copied from interface:Promise
Submits the provided runnable for execution once thisPromise
has completed, and regardless of whether of its outcome.This method can be used for resource cleanup after a series of asynchronous tasks have completed. More specifically, this method should be used in a similar manner to
finally
statements intry...catch
expressions.This method is equivalent to
Promise.thenAlways(Runnable)
.- Specified by:
thenFinally
in interfacePromise<V,
E extends Exception> - Parameters:
onFinally
- The runnable which will be notified regardless of the final outcome of thisPromise
.- Returns:
- A
Promise
that is guaranteed to be completed once the provided callback has been executed.
-
thenAsync
Description copied from interface:Promise
Submits the provided asynchronous function for execution once thisPromise
has completed with a result, and returns a newPromise
representing the outcome of the function. If thisPromise
complete with an exception then the function will not be invoked and the error will be forwarded to the returnedPromise
.This method may be used for chaining together a series of asynchronous tasks.
- Specified by:
thenAsync
in interfacePromise<V,
E extends Exception> - Type Parameters:
VOUT
- The type of the function's result, orVoid
if the function does not return anything (i.e. it only has side-effects). Note that the type may be different to the type of thisPromise
.- Parameters:
onResult
- The asynchronous function which will be executed upon successful completion of thisPromise
.- Returns:
- A new
Promise
representing the outcome of the function.
-
thenCatchAsync
public final <EOUT extends Exception> Promise<V,EOUT> thenCatchAsync(AsyncFunction<? super E, V, EOUT> onException) Description copied from interface:Promise
Submits the provided asynchronous function for execution once thisPromise
has completed with an exception, and returns a newPromise
representing the outcome of the function. If thisPromise
completes with a result then the function will not be invoked and the exception will be forwarded to the returnedPromise
.This method may be used for chaining together a series of asynchronous tasks.
- Specified by:
thenCatchAsync
in interfacePromise<V,
E extends Exception> - Type Parameters:
EOUT
- The type of the exception thrown by the function if it fails, orNeverThrowsException
if it cannot fails. Note that the type may be different to the type of thisPromise
.- Parameters:
onException
- The asynchronous function which will be executed upon failure completion of thisPromise
.- Returns:
- A new
Promise
representing the outcome of the function.
-
thenCatchRuntimeExceptionAsync
public final Promise<V,E> thenCatchRuntimeExceptionAsync(AsyncFunction<? super RuntimeException, V, E> onRuntimeException) Description copied from interface:Promise
Submits the provided asynchronous function for execution once thisPromise
has completed with aRuntimeException
, and returns a newPromise
representing the outcome of the function. If thisPromise
completes with a result or the typed exception then the completion asynchronous function will not be called.This method may be used for chaining together a series of asynchronous tasks.
- Specified by:
thenCatchRuntimeExceptionAsync
in interfacePromise<V,
E extends Exception> - Parameters:
onRuntimeException
- The asynchronous function which will be executed upon failure completion with aRuntimeException
of thisPromise
.- Returns:
- A new
Promise
representing the outcome of the function.
-
thenAsync
public final <VOUT,EOUT extends Exception> Promise<VOUT,EOUT> thenAsync(AsyncFunction<? super V, VOUT, EOUT> onResult, AsyncFunction<? super E, VOUT, EOUT> onException) Description copied from interface:Promise
Submits the provided asynchronous functions for execution once thisPromise
has completed, and returns a newPromise
representing the outcome of the invoked function. If thisPromise
completes with a result thenonResult
will be invoked with the result, otherwiseonException
will be invoked with the exception that occurred.This method may be used for chaining together a series of asynchronous tasks.
- Specified by:
thenAsync
in interfacePromise<V,
E extends Exception> - Type Parameters:
VOUT
- The type of the functions' result, orVoid
if the functions do not return anything (i.e. they only have side-effects). Note that the type may be different to the type of thisPromise
.EOUT
- The type of the exception thrown by the functions if they fail, orNeverThrowsException
if they cannot fail. Note that the type may be different to the type of thisPromise
.- Parameters:
onResult
- The asynchronous function which will be executed upon successful completion of thisPromise
.onException
- The asynchronous function which will be executed upon failure of thisPromise
.- Returns:
- A new
Promise
representing the outcome of the invoked function.
-
thenAsync
public final <VOUT,EOUT extends Exception> Promise<VOUT,EOUT> thenAsync(AsyncFunction<? super V, VOUT, EOUT> onResult, AsyncFunction<? super E, VOUT, EOUT> onException, AsyncFunction<? super RuntimeException, VOUT, EOUT> onRuntimeException) Description copied from interface:Promise
Submits the provided asynchronous functions for execution once thisPromise
has completed, and returns a newPromise
representing the outcome of the invoked function. If thisPromise
completes with a result thenonResult
will be invoked with the result, otherwiseonException
will be invoked with the exception that occurred, oronRuntimeException
will be invoked with the runtime exception that occurred.This method may be used for chaining together a series of asynchronous tasks.
- Specified by:
thenAsync
in interfacePromise<V,
E extends Exception> - Type Parameters:
VOUT
- The type of the functions' result, orVoid
if the functions do not return anything (i.e. they only have side-effects). Note that the type may be different to the type of thisPromise
.EOUT
- The type of the exception thrown by the functions if they fail, orNeverThrowsException
if they cannot fail. Note that the type may be different to the type of thisPromise
.- Parameters:
onResult
- The asynchronous function which will be executed upon successful completion of thisPromise
.onException
- The asynchronous function which will be executed upon failure of thisPromise
.onRuntimeException
- The asynchronous function which will be executed upon failure withRuntimeException
of thisPromise
.- Returns:
- A new
Promise
representing the outcome of the invoked function.
-
thenOnRuntimeException
Description copied from interface:Promise
Registers the provided completion handler for notification if thisPromise
cannot be completed due to an runtime exception. If thisPromise
completes with a result or the typed exception then the completion handler will not be notified.This method can be used for asynchronous completion notification.
- Specified by:
thenOnRuntimeException
in interfacePromise<V,
E extends Exception> - Parameters:
onRuntimeException
- The completion handler which will be notified upon an uncaught runtime exception completion of thisPromise
.- Returns:
- A
Promise
that is guaranteed to be completed once the provided callback has been executed.
-
thenOnCompletion
public final <T extends ResultHandler<? super V> & ExceptionHandler<? super E> & RuntimeExceptionHandler> Promise<V,E> thenOnCompletion(T handler) Description copied from interface:Promise
Registers the provided completion handler for notification for all completion cases of thisPromise
.This method can be used for asynchronous completion notification.
- Specified by:
thenOnCompletion
in interfacePromise<V,
E extends Exception> - Type Parameters:
T
- the composite type of the handler for handling all completion cases- Parameters:
handler
- The handler that will be called once thisPromise
is completed.- Returns:
- A
Promise
that is guaranteed to be completed once the provided callback has been executed.
-
tryCancel
Invoked when the client attempts to cancel the asynchronous task represented by this promise. Implementations which support cancellation should override this method to cancel the asynchronous task and, if successful, return an appropriate exception which can be used to signal that the task has failed.By default cancellation is not supported and this method returns
null
.- Parameters:
mayInterruptIfRunning
-true
if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete.- Returns:
null
if cancellation was not supported or not possible, otherwise an appropriate exception.
-