Interface Promise<T>

All Superinterfaces:
Filter<Promise,T,Promise<T>>, HK<Promise,T,Promise<T>>
All Known Implementing Classes:
PromisesSet, RunnablePromise, SolvablePromise, SolvedPromise

public interface Promise<T> extends Filter<Promise,T,Promise<T>>, HK<Promise,T,Promise<T>>
A promise is a component denoting an asynchronous computation. Such a component can be mapped to chain transformations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final java.time.Duration
    The maximum default await duration is 3 minutes and 14 seconds
  • Method Summary

    Modifier and Type
    Method
    Description
    default <R> Promise<R>
    and(FunctionWithError<? super T,R> function)
    Method used when a new computation must be done when the current one succeeds.
    default T
    Method called when we want the result in a direct style.
    default T
    await(long duration, TimeUnit unit)
    Method 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>
    flatmap(java.util.function.Function<? super T,Promise<R>> function)
    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 terminates
    onFailure(java.util.function.Consumer<Throwable> consumer)
    Method called when the computation fails
    onSuccess(java.util.function.Consumer<T> consumer)
    Method called when the computation succeeds
    static <T> Promise<T>
    pure(T value)
    Constructor
    default <R> Promise<R>
    then(java.util.function.Function<? super T,Promise<R>> function)
    Method used when a new asynchronous computation must be done when the current one succeeds.

    Methods inherited from interface org.smallibs.control.Filter

    filter

    Methods inherited from interface org.smallibs.type.HK

    apply, self
  • Field Details

    • MAX_AWAIT_DURATION

      static final java.time.Duration MAX_AWAIT_DURATION
      The maximum default await duration is 3 minutes and 14 seconds
  • Method Details

    • pure

      static <T> Promise<T> pure(T value)
      Constructor
      Type Parameters:
      T - The value type returned by this promise
      Parameters:
      value - The captured value
      Returns:
      a solved promise
    • getFuture

      @Deprecated Future<T> getFuture()
      Deprecated.
      Provides the underlying future able to capture and returns the result or the error for a given execution
      Returns:
      a future
    • await

      default T await() throws Throwable
      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

      default T await(long duration, TimeUnit unit) throws Throwable
      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

      T await(java.time.Duration duration) throws Throwable
      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

      Promise<T> onSuccess(java.util.function.Consumer<T> consumer)
      Method called when the computation succeeds
      Parameters:
      consumer - The callback to be activated on success
      Returns:
      the current promise
    • onFailure

      Promise<T> onFailure(java.util.function.Consumer<Throwable> consumer)
      Method called when the computation fails
      Parameters:
      consumer - The callback to be activated on error
      Returns:
      the current promise
    • onComplete

      Promise<T> onComplete(java.util.function.Consumer<Try<T>> consumer)
      Callback called when the computation terminates
      Parameters:
      consumer - The callback to be activated on completion
      Returns:
      the current promise
    • map

      <R> Promise<R> map(FunctionWithError<? super T,? extends R> function)
      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 error
      onError - The function to apply on error which can also raise an error
      Returns:
      a new promise
    • and

      default <R> Promise<R> and(FunctionWithError<? super T,R> function)
      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

      <R> Promise<R> flatmap(java.util.function.Function<? super T,Promise<R>> function)
      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

      default <R> Promise<R> then(java.util.function.Function<? super T,Promise<R>> function)
      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