Package dev.webfx.platform.async
Interface Promise<T>
- All Superinterfaces:
Handler<AsyncResult<T>>
- All Known Subinterfaces:
PromiseInternal<T>
- All Known Implementing Classes:
PromiseImpl
Represents the writable side of an action that may, or may not, have occurred yet.
The future()
method returns the Future
associated with a promise, the future
can be used for getting notified of the promise completion and retrieve its value.
A promise extends Handler<AsyncResult<T>>
so it can be used as a callback.
- Author:
- Julien Viet
-
Method Summary
Modifier and TypeMethodDescriptiondefault void
complete()
Callscomplete(null)
default void
Set the result.default void
Callsfail(Throwable)
with themessage
.default void
Set the failure.future()
default void
handle
(AsyncResult<T> asyncResult) Succeed or fail this promise with theAsyncResult
event.static <T> Promise<T>
promise()
Create a promise that hasn't completed yetdefault boolean
CallstryComplete(null)
.boolean
tryComplete
(T result) Likecomplete(Object)
but returnsfalse
when the promise is already completed instead of throwing anIllegalStateException
, it returnstrue
otherwise.default boolean
Callsfail(Throwable)
with themessage
.boolean
Likefail(Throwable)
but returnsfalse
when the promise is already completed instead of throwing anIllegalStateException
, it returnstrue
otherwise.
-
Method Details
-
promise
Create a promise that hasn't completed yet- Type Parameters:
T
- the result type- Returns:
- the promise
-
handle
Succeed or fail this promise with theAsyncResult
event. -
complete
Set the result. Any handler will be called, if there is one, and the promise will be marked as completed. Any handler set on the associated promise will be called.- Parameters:
result
- the result- Throws:
IllegalStateException
- when the promise is already completed
-
complete
default void complete()Callscomplete(null)
- Throws:
IllegalStateException
- when the promise is already completed
-
fail
Set the failure. Any handler will be called, if there is one, and the future will be marked as completed.- Parameters:
cause
- the failure cause- Throws:
IllegalStateException
- when the promise is already completed
-
fail
Callsfail(Throwable)
with themessage
.- Parameters:
message
- the failure message- Throws:
IllegalStateException
- when the promise is already completed
-
tryComplete
Likecomplete(Object)
but returnsfalse
when the promise is already completed instead of throwing anIllegalStateException
, it returnstrue
otherwise.- Parameters:
result
- the result- Returns:
false
when the future is already completed
-
tryComplete
default boolean tryComplete()CallstryComplete(null)
.- Returns:
false
when the future is already completed
-
tryFail
Likefail(Throwable)
but returnsfalse
when the promise is already completed instead of throwing anIllegalStateException
, it returnstrue
otherwise.- Parameters:
cause
- the failure cause- Returns:
false
when the future is already completed
-
tryFail
Callsfail(Throwable)
with themessage
.- Parameters:
message
- the failure message- Returns:
- false when the future is already completed
-
future
- Returns:
- the
Future
associated with this promise, it can be used to be aware of the promise completion
-