Package org.smallibs.concurrent.promise
Interface Promise<T>
- All Known Implementing Classes:
PromisesSet,RunnablePromise,SolvablePromise,SolvedPromise
A promise is a component denoting an asynchronous computation. Such component can be mapped in order to chain
transformations.
-
Method Summary
Modifier and TypeMethodDescriptiondefault <R> Promise<R> and(FunctionWithError<? super T, R> function) Method used when a new computation must be done when the current one succeed.await()This method waits for the result.await(java.time.Duration duration) This method waits for the result for a maximum duration.<R> Promise<R> biMap(FunctionWithError<? super T, ? extends R> onSuccess, FunctionWithError<? super Throwable, ? extends R> onError) Method used to map a function on success and another one on error.<R> Promise<R> Method used to flatmap a function.Provides the underlying future able to capture and returns the result or the error for a given execution<R> Promise<R> map(FunctionWithError<? super T, ? extends R> function) Method used to map a function.onComplete(java.util.function.Consumer<Try<T>> consumer) Callback called when the computation terminatesMethod called when the computation failsMethod called when the computation succeedsstatic <T> Promise<T> pure(T value) Constructordefault <R> Promise<R> Method used when a new asynchronous computation must be done when the current one succeed.
-
Method Details
-
pure
Constructor- Type Parameters:
T- The value type returned by this promise- Parameters:
value- The captured value- Returns:
- a solved promise
-
getFuture
Provides the underlying future able to capture and returns the result or the error for a given execution- Returns:
- a future
-
await
This method waits for the result. This method uses a sleep/interrupt technic.- Throws:
Exception
-
await
This method waits for the result for a maximum duration. This method uses a sleep/interrupt technic.- Throws:
Exception
-
onSuccess
Method called when the computation succeeds- Parameters:
consumer- The callback to be activated on success- Returns:
- the current promise
-
onFailure
Method called when the computation fails- Parameters:
consumer- The callback to be activated on error- Returns:
- the current promise
-
onComplete
Callback called when the computation terminates- Parameters:
consumer- The callback to be activated on completion- Returns:
- the current promise
-
map
Method used to map a function. This mapping is done when the operation is a success. The result of this mapping is a new promise component.- Type Parameters:
R- the promised value type- Parameters:
function- The function to applied on success which can raise an error- Returns:
- a new promise
-
biMap
<R> Promise<R> biMap(FunctionWithError<? super T, ? extends R> onSuccess, FunctionWithError<? super Throwable, ? extends R> onError) Method used to map a function on success and another one on error. The result of this mapping is a new promise component.- Type Parameters:
R- the promised value type- Parameters:
onSuccess- The function to applied on success which can raise an erroronError- The function to applied on error which can also raise an error- Returns:
- a new promise
-
and
Method used when a new computation must be done when the current one succeed. The current one and the chained one are done sequentially in the same context.- Type Parameters:
R- the promised value type- Parameters:
function- The function to applied on success- Returns:
- a new promise
-
flatmap
Method used to flatmap a function. This mapping is done when the operation is a success. The result of this mapping is a new promise component.- Type Parameters:
R- the promised value type- Parameters:
function- The function to applied on success- Returns:
- a new promise
-
then
Method used when a new asynchronous computation must be done when the current one succeed. The current one and the chained one are not done sequentially in the same context.- Type Parameters:
R- the promised value type- Parameters:
function- The function to applied on success- Returns:
- a new promise
-