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 a component can
be mapped to chain transformations.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final java.time.DurationThe maximum default await duration is 3 minutes and 14 seconds -
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 succeeds.default Tawait()Method called when we want the result in a direct style.default TMethod called when we want the result in a direct style.await(java.time.Duration duration) Method called when we want the result in a direct style.<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.Deprecated.<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 succeeds.
-
Field Details
-
MAX_AWAIT_DURATION
static final java.time.Duration MAX_AWAIT_DURATIONThe maximum default await duration is 3 minutes and 14 seconds
-
-
Method Details
-
pure
Constructor- Type Parameters:
T- The value type returned by this promise- Parameters:
value- The captured value- Returns:
- a solved promise
-
getFuture
Deprecated.Provides the underlying future able to capture and returns the result or the error for a given execution- Returns:
- a future
-
await
Method called when we want the result in a direct style.This method waits for the result for a maximum duration. With virtual thread, this await method parks the current thread (if not pinned) and releases the carrier thread.
- Returns:
- the value or throw an error.
- Throws:
Throwable
-
await
Method called when we want the result in a direct style.This method waits for the result for a maximum duration. With virtual thread, this await method parks the current thread (if not pinned) and releases the carrier thread.
- Returns:
- the value or throw an error.
- Throws:
Throwable
-
await
Method called when we want the result in a direct style.This method waits for the result for a maximum duration. With virtual thread, this await method parks the current thread (if not pinned) and releases the carrier thread.
- Returns:
- the value or throw an error.
- Throws:
Throwable
-
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 apply 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 apply on success which can raise an erroronError- The function to apply 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 succeeds. 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 apply 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 apply on success- Returns:
- a new promise
-
then
Method used when a new asynchronous computation must be done when the current one succeeds. 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 apply on success- Returns:
- a new promise
-