public class Promise<T> extends Object
Since then(Functor, Functor)
, except(Functor)
,
and always(Functor)
take Functors as parameters, which must have a return value, this implementation
provides convenience wrappers onSuccess(SuccessCallback)
, onFail(SuccessCallback)
,
and onComplete(SuccessCallback)
which take SuccessCallback
objects instead. For simple cases,
these wrappers will be easier to use because you don't need to return a dummy null at the end of the callback.
For more complex cases, where the return value of one Functor is meant to be piped into the subsequent Functor, then the Functor variants should be used.
Modifier and Type | Class and Description |
---|---|
static class |
Promise.State
Encapsulates the state of a Promise.
|
Constructor and Description |
---|
Promise(ExecutorFunction executor)
Creates a new promise with the given executor function.
|
Modifier and Type | Method and Description |
---|---|
static Promise |
all(Promise... promises)
The Promise.all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises.
|
static Promise |
allSettled(Promise... promises)
The Promise.allSettled() method returns a promise that resolves after all
of the given promises have either fulfilled or rejected, with an array of
objects that each describes the outcome of each promise.
|
Promise |
always(Functor handlerFunc)
Implementation of Promise.finally().
|
AsyncResource<T> |
asAsyncResource() |
T |
await()
Uses invokeAndBlock to wait for this promise to be either resolved or rejected.
|
Promise |
except(Functor<Throwable,?> rejectionFunc)
Implementation of Promise.catch().
|
Promise.State |
getState()
Returns the current state of the promise.
|
T |
getValue()
Gets the return value once the promise is fulfilled.
|
Promise |
onComplete(SuccessCallback handlerFunc)
|
Promise |
onFail(SuccessCallback<Throwable> rejectionFunc)
|
Promise |
onSuccess(SuccessCallback<T> resolutionFunc)
|
static <V> Promise<V> |
promisify(AsyncResource<V> res) |
Promise |
ready(SuccessCallback<T> resolutionFunc,
SuccessCallback<Throwable> rejectionFunc)
|
static Promise |
reject(Throwable err) |
static <V> Promise<V> |
resolve(V value) |
Promise |
then(Functor<T,?> resolutionFunc) |
Promise |
then(Functor<T,?> resolutionFunc,
Functor<Throwable,?> rejectionFunc)
The then() method returns a Promise.
|
public Promise(ExecutorFunction executor)
executor
- The executor function. This is executed immediately, and should call either the passed resolve
or reject functor to mark success or failure.public Promise ready(SuccessCallback<T> resolutionFunc, SuccessCallback<Throwable> rejectionFunc)
resolutionFunc
- Callback to run on resolution of promise.rejectionFunc
- Callback to run on rejection of promise.public Promise onSuccess(SuccessCallback<T> resolutionFunc)
resolutionFunc
- Callback called when project is fulfilled.public Promise onFail(SuccessCallback<Throwable> rejectionFunc)
rejectionFunc
- public Promise onComplete(SuccessCallback handlerFunc)
handlerFunc
- public Promise then(Functor<T,?> resolutionFunc, Functor<Throwable,?> rejectionFunc)
resolutionFunc
- A Function called if the Promise is fulfilled. This function has one argument, the fulfillment value. If it is null, it is internally replaced with an "Identity" function (it returns the received argument).rejectionFunc
- A Function called if the Promise is rejected. This function has one argument, the rejection reason. If it is null, it is internally replaced with a "Thrower" function (it throws an error it received as argument).Once a Promise is fulfilled or rejected, the respective handler function (resolutionFunc or rejectionFunc) will be called asynchronously (scheduled on the EDT). The behavior of the handler function follows a specific set of rules. If a handler function:
public Promise except(Functor<Throwable,?> rejectionFunc)
rejectionFunc
- Function called if promise is rejected.public Promise always(Functor handlerFunc)
handlerFunc
- public T getValue()
public Promise.State getState()
public static Promise all(Promise... promises)
promises
- public static Promise allSettled(Promise... promises)
The Promise.allSettled() method returns a promise that resolves after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.
It is typically used when you have multiple asynchronous tasks that are not dependent on one another to complete successfully, or you'd always like to know the result of each promise.
In comparison, the Promise returned by Promise.all() may be more appropriate if the tasks are dependent on each other / if you'd like to immediately reject upon any of them rejecting.
promises
- public T await()
AsyncResource.AsyncExecutionException
if the
promise failed. Otherwise it will return the resolved value.public static <V> Promise<V> resolve(V value)
public static <V> Promise<V> promisify(AsyncResource<V> res)
public AsyncResource<T> asAsyncResource()