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 component can be mapped in order to chain transformations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final java.time.Duration
     
  • 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 succeed.
    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>
    flatmap(java.util.function.Function<? super T,Promise<R>> function)
    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 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 succeed.

    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
  • 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

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

      T await() throws Exception
      This method waits for the result. This method uses a sleep/interrupt technic.
      Throws:
      Exception
    • await

      T await(java.time.Duration duration) throws Exception
      This method waits for the result for a maximum duration. This method uses a sleep/interrupt technic.
      Throws:
      Exception
    • 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 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 error
      onError - The function to applied 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 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

      <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 applied 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 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