Most visited

Recently visited

Added in API level 24

CompletionStage

public interface CompletionStage

java.util.concurrent.CompletionStage<T>
Known Indirect Subclasses


A stage of a possibly asynchronous computation, that performs an action or computes a value when another CompletionStage completes. A stage completes upon termination of its computation, but this may in turn trigger other dependent stages. The functionality defined in this interface takes only a few basic forms, which expand out to a larger set of methods to capture a range of usage styles:

All methods adhere to the above triggering, execution, and exceptional completion specifications (which are not repeated in individual method specifications). Additionally, while arguments used to pass a completion result (that is, for parameters of type T) for methods accepting them may be null, passing a null value for any other parameter will result in a NullPointerException being thrown.

Method form handle is the most general way of creating a continuation stage, unconditionally performing a computation that is given both the result and exception (if any) of the triggering CompletionStage, and computing an arbitrary result. Method whenComplete is similar, but preserves the result of the triggering stage instead of computing a new one. Because a stage's normal result may be null, both methods should have a computation structured thus:

(result, exception) -> {
   if (exception == null) {
     // triggering stage completed normally
   } else {
     // triggering stage completed exceptionally
   }
 }

This interface does not define methods for initially creating, forcibly completing normally or exceptionally, probing completion status or results, or awaiting completion of a stage. Implementations of CompletionStage may provide means of achieving such effects, as appropriate. Method toCompletableFuture() enables interoperability among different implementations of this interface by providing a common conversion type.

Summary

Public methods

abstract CompletionStage<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action)

Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed with the corresponding result as argument to the supplied action.

abstract CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor)

Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed using the supplied executor, with the corresponding result as argument to the supplied action.

abstract CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action)

Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed using this stage's default asynchronous execution facility, with the corresponding result as argument to the supplied action.

abstract <U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn)

Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed with the corresponding result as argument to the supplied function.

abstract <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor)

Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed using the supplied executor, with the corresponding result as argument to the supplied function.

abstract <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn)

Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed using this stage's default asynchronous execution facility, with the corresponding result as argument to the supplied function.

abstract CompletionStage<T> exceptionally(Function<Throwable, ? extends T> fn)

Returns a new CompletionStage that, when this stage completes exceptionally, is executed with this stage's exception as the argument to the supplied function.

abstract <U> CompletionStage<U> handle(BiFunction<? super T, Throwable, ? extends U> fn)

Returns a new CompletionStage that, when this stage completes either normally or exceptionally, is executed with this stage's result and exception as arguments to the supplied function.

abstract <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor)

Returns a new CompletionStage that, when this stage completes either normally or exceptionally, is executed using the supplied executor, with this stage's result and exception as arguments to the supplied function.

abstract <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn)

Returns a new CompletionStage that, when this stage completes either normally or exceptionally, is executed using this stage's default asynchronous execution facility, with this stage's result and exception as arguments to the supplied function.

abstract CompletionStage<Void> runAfterBoth(CompletionStage<?> other, Runnable action)

Returns a new CompletionStage that, when this and the other given stage both complete normally, executes the given action.

abstract CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action)

Returns a new CompletionStage that, when this and the other given stage both complete normally, executes the given action using this stage's default asynchronous execution facility.

abstract CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor)

Returns a new CompletionStage that, when this and the other given stage both complete normally, executes the given action using the supplied executor.

abstract CompletionStage<Void> runAfterEither(CompletionStage<?> other, Runnable action)

Returns a new CompletionStage that, when either this or the other given stage complete normally, executes the given action.

abstract CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action)

Returns a new CompletionStage that, when either this or the other given stage complete normally, executes the given action using this stage's default asynchronous execution facility.

abstract CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor)

Returns a new CompletionStage that, when either this or the other given stage complete normally, executes the given action using the supplied executor.

abstract CompletionStage<Void> thenAccept(Consumer<? super T> action)

Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied action.

abstract CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor)

Returns a new CompletionStage that, when this stage completes normally, is executed using the supplied Executor, with this stage's result as the argument to the supplied action.

abstract CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action)

Returns a new CompletionStage that, when this stage completes normally, is executed using this stage's default asynchronous execution facility, with this stage's result as the argument to the supplied action.

abstract <U> CompletionStage<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)

Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed with the two results as arguments to the supplied action.

abstract <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor)

Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed using the supplied executor, with the two results as arguments to the supplied action.

abstract <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)

Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed using this stage's default asynchronous execution facility, with the two results as arguments to the supplied action.

abstract <U> CompletionStage<U> thenApply(Function<? super T, ? extends U> fn)

Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied function.

abstract <U> CompletionStage<U> thenApplyAsync(Function<? super T, ? extends U> fn)

Returns a new CompletionStage that, when this stage completes normally, is executed using this stage's default asynchronous execution facility, with this stage's result as the argument to the supplied function.

abstract <U> CompletionStage<U> thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor)

Returns a new CompletionStage that, when this stage completes normally, is executed using the supplied Executor, with this stage's result as the argument to the supplied function.

abstract <U, V> CompletionStage<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn)

Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed with the two results as arguments to the supplied function.

abstract <U, V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn)

Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed using this stage's default asynchronous execution facility, with the two results as arguments to the supplied function.

abstract <U, V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn, Executor executor)

Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed using the supplied executor, with the two results as arguments to the supplied function.

abstract <U> CompletionStage<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn)

Returns a new CompletionStage that is completed with the same value as the CompletionStage returned by the given function.

abstract <U> CompletionStage<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor)

Returns a new CompletionStage that is completed with the same value as the CompletionStage returned by the given function, executed using the supplied Executor.

abstract <U> CompletionStage<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn)

Returns a new CompletionStage that is completed with the same value as the CompletionStage returned by the given function, executed using this stage's default asynchronous execution facility.

abstract CompletionStage<Void> thenRun(Runnable action)

Returns a new CompletionStage that, when this stage completes normally, executes the given action.

abstract CompletionStage<Void> thenRunAsync(Runnable action, Executor executor)

Returns a new CompletionStage that, when this stage completes normally, executes the given action using the supplied Executor.

abstract CompletionStage<Void> thenRunAsync(Runnable action)

Returns a new CompletionStage that, when this stage completes normally, executes the given action using this stage's default asynchronous execution facility.

abstract CompletableFuture<T> toCompletableFuture()

Returns a CompletableFuture maintaining the same completion properties as this stage.

abstract CompletionStage<T> whenComplete(BiConsumer<? super T, ? super Throwable> action)

Returns a new CompletionStage with the same result or exception as this stage, that executes the given action when this stage completes.

abstract CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action)

Returns a new CompletionStage with the same result or exception as this stage, that executes the given action using this stage's default asynchronous execution facility when this stage completes.

abstract CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor)

Returns a new CompletionStage with the same result or exception as this stage, that executes the given action using the supplied Executor when this stage completes.

Public methods

acceptEither

Added in API level 24
CompletionStage<Void> acceptEither (CompletionStage<? extends T> other, 
                Consumer<? super T> action)

Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed with the corresponding result as argument to the supplied action. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
action Consumer: the action to perform before completing the returned CompletionStage
Returns
CompletionStage<Void> the new CompletionStage

acceptEitherAsync

Added in API level 24
CompletionStage<Void> acceptEitherAsync (CompletionStage<? extends T> other, 
                Consumer<? super T> action, 
                Executor executor)

Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed using the supplied executor, with the corresponding result as argument to the supplied action. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
action Consumer: the action to perform before completing the returned CompletionStage
executor Executor: the executor to use for asynchronous execution
Returns
CompletionStage<Void> the new CompletionStage

acceptEitherAsync

Added in API level 24
CompletionStage<Void> acceptEitherAsync (CompletionStage<? extends T> other, 
                Consumer<? super T> action)

Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed using this stage's default asynchronous execution facility, with the corresponding result as argument to the supplied action. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
action Consumer: the action to perform before completing the returned CompletionStage
Returns
CompletionStage<Void> the new CompletionStage

applyToEither

Added in API level 24
CompletionStage<U> applyToEither (CompletionStage<? extends T> other, 
                Function<? super T, U> fn)

Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed with the corresponding result as argument to the supplied function. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
fn Function: the function to use to compute the value of the returned CompletionStage
Returns
CompletionStage<U> the new CompletionStage

applyToEitherAsync

Added in API level 24
CompletionStage<U> applyToEitherAsync (CompletionStage<? extends T> other, 
                Function<? super T, U> fn, 
                Executor executor)

Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed using the supplied executor, with the corresponding result as argument to the supplied function. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
fn Function: the function to use to compute the value of the returned CompletionStage
executor Executor: the executor to use for asynchronous execution
Returns
CompletionStage<U> the new CompletionStage

applyToEitherAsync

Added in API level 24
CompletionStage<U> applyToEitherAsync (CompletionStage<? extends T> other, 
                Function<? super T, U> fn)

Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed using this stage's default asynchronous execution facility, with the corresponding result as argument to the supplied function. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
fn Function: the function to use to compute the value of the returned CompletionStage
Returns
CompletionStage<U> the new CompletionStage

exceptionally

Added in API level 24
CompletionStage<T> exceptionally (Function<Throwable, ? extends T> fn)

Returns a new CompletionStage that, when this stage completes exceptionally, is executed with this stage's exception as the argument to the supplied function. Otherwise, if this stage completes normally, then the returned stage also completes normally with the same value.

Parameters
fn Function: the function to use to compute the value of the returned CompletionStage if this CompletionStage completed exceptionally
Returns
CompletionStage<T> the new CompletionStage

handle

Added in API level 24
CompletionStage<U> handle (BiFunction<? super T, Throwable, ? extends U> fn)

Returns a new CompletionStage that, when this stage completes either normally or exceptionally, is executed with this stage's result and exception as arguments to the supplied function.

When this stage is complete, the given function is invoked with the result (or null if none) and the exception (or null if none) of this stage as arguments, and the function's result is used to complete the returned stage.

Parameters
fn BiFunction: the function to use to compute the value of the returned CompletionStage
Returns
CompletionStage<U> the new CompletionStage

handleAsync

Added in API level 24
CompletionStage<U> handleAsync (BiFunction<? super T, Throwable, ? extends U> fn, 
                Executor executor)

Returns a new CompletionStage that, when this stage completes either normally or exceptionally, is executed using the supplied executor, with this stage's result and exception as arguments to the supplied function.

When this stage is complete, the given function is invoked with the result (or null if none) and the exception (or null if none) of this stage as arguments, and the function's result is used to complete the returned stage.

Parameters
fn BiFunction: the function to use to compute the value of the returned CompletionStage
executor Executor: the executor to use for asynchronous execution
Returns
CompletionStage<U> the new CompletionStage

handleAsync

Added in API level 24
CompletionStage<U> handleAsync (BiFunction<? super T, Throwable, ? extends U> fn)

Returns a new CompletionStage that, when this stage completes either normally or exceptionally, is executed using this stage's default asynchronous execution facility, with this stage's result and exception as arguments to the supplied function.

When this stage is complete, the given function is invoked with the result (or null if none) and the exception (or null if none) of this stage as arguments, and the function's result is used to complete the returned stage.

Parameters
fn BiFunction: the function to use to compute the value of the returned CompletionStage
Returns
CompletionStage<U> the new CompletionStage

runAfterBoth

Added in API level 24
CompletionStage<Void> runAfterBoth (CompletionStage<?> other, 
                Runnable action)

Returns a new CompletionStage that, when this and the other given stage both complete normally, executes the given action. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
action Runnable: the action to perform before completing the returned CompletionStage
Returns
CompletionStage<Void> the new CompletionStage

runAfterBothAsync

Added in API level 24
CompletionStage<Void> runAfterBothAsync (CompletionStage<?> other, 
                Runnable action)

Returns a new CompletionStage that, when this and the other given stage both complete normally, executes the given action using this stage's default asynchronous execution facility. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
action Runnable: the action to perform before completing the returned CompletionStage
Returns
CompletionStage<Void> the new CompletionStage

runAfterBothAsync

Added in API level 24
CompletionStage<Void> runAfterBothAsync (CompletionStage<?> other, 
                Runnable action, 
                Executor executor)

Returns a new CompletionStage that, when this and the other given stage both complete normally, executes the given action using the supplied executor. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
action Runnable: the action to perform before completing the returned CompletionStage
executor Executor: the executor to use for asynchronous execution
Returns
CompletionStage<Void> the new CompletionStage

runAfterEither

Added in API level 24
CompletionStage<Void> runAfterEither (CompletionStage<?> other, 
                Runnable action)

Returns a new CompletionStage that, when either this or the other given stage complete normally, executes the given action. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
action Runnable: the action to perform before completing the returned CompletionStage
Returns
CompletionStage<Void> the new CompletionStage

runAfterEitherAsync

Added in API level 24
CompletionStage<Void> runAfterEitherAsync (CompletionStage<?> other, 
                Runnable action)

Returns a new CompletionStage that, when either this or the other given stage complete normally, executes the given action using this stage's default asynchronous execution facility. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
action Runnable: the action to perform before completing the returned CompletionStage
Returns
CompletionStage<Void> the new CompletionStage

runAfterEitherAsync

Added in API level 24
CompletionStage<Void> runAfterEitherAsync (CompletionStage<?> other, 
                Runnable action, 
                Executor executor)

Returns a new CompletionStage that, when either this or the other given stage complete normally, executes the given action using the supplied executor. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
action Runnable: the action to perform before completing the returned CompletionStage
executor Executor: the executor to use for asynchronous execution
Returns
CompletionStage<Void> the new CompletionStage

thenAccept

Added in API level 24
CompletionStage<Void> thenAccept (Consumer<? super T> action)

Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied action. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
action Consumer: the action to perform before completing the returned CompletionStage
Returns
CompletionStage<Void> the new CompletionStage

thenAcceptAsync

Added in API level 24
CompletionStage<Void> thenAcceptAsync (Consumer<? super T> action, 
                Executor executor)

Returns a new CompletionStage that, when this stage completes normally, is executed using the supplied Executor, with this stage's result as the argument to the supplied action. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
action Consumer: the action to perform before completing the returned CompletionStage
executor Executor: the executor to use for asynchronous execution
Returns
CompletionStage<Void> the new CompletionStage

thenAcceptAsync

Added in API level 24
CompletionStage<Void> thenAcceptAsync (Consumer<? super T> action)

Returns a new CompletionStage that, when this stage completes normally, is executed using this stage's default asynchronous execution facility, with this stage's result as the argument to the supplied action. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
action Consumer: the action to perform before completing the returned CompletionStage
Returns
CompletionStage<Void> the new CompletionStage

thenAcceptBoth

Added in API level 24
CompletionStage<Void> thenAcceptBoth (CompletionStage<? extends U> other, 
                BiConsumer<? super T, ? super U> action)

Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed with the two results as arguments to the supplied action. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
action BiConsumer: the action to perform before completing the returned CompletionStage
Returns
CompletionStage<Void> the new CompletionStage

thenAcceptBothAsync

Added in API level 24
CompletionStage<Void> thenAcceptBothAsync (CompletionStage<? extends U> other, 
                BiConsumer<? super T, ? super U> action, 
                Executor executor)

Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed using the supplied executor, with the two results as arguments to the supplied action. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
action BiConsumer: the action to perform before completing the returned CompletionStage
executor Executor: the executor to use for asynchronous execution
Returns
CompletionStage<Void> the new CompletionStage

thenAcceptBothAsync

Added in API level 24
CompletionStage<Void> thenAcceptBothAsync (CompletionStage<? extends U> other, 
                BiConsumer<? super T, ? super U> action)

Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed using this stage's default asynchronous execution facility, with the two results as arguments to the supplied action. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
action BiConsumer: the action to perform before completing the returned CompletionStage
Returns
CompletionStage<Void> the new CompletionStage

thenApply

Added in API level 24
CompletionStage<U> thenApply (Function<? super T, ? extends U> fn)

Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied function.

This method is analogous to Optional.map and Stream.map.

See the CompletionStage documentation for rules covering exceptional completion.

Parameters
fn Function: the function to use to compute the value of the returned CompletionStage
Returns
CompletionStage<U> the new CompletionStage

thenApplyAsync

Added in API level 24
CompletionStage<U> thenApplyAsync (Function<? super T, ? extends U> fn)

Returns a new CompletionStage that, when this stage completes normally, is executed using this stage's default asynchronous execution facility, with this stage's result as the argument to the supplied function. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
fn Function: the function to use to compute the value of the returned CompletionStage
Returns
CompletionStage<U> the new CompletionStage

thenApplyAsync

Added in API level 24
CompletionStage<U> thenApplyAsync (Function<? super T, ? extends U> fn, 
                Executor executor)

Returns a new CompletionStage that, when this stage completes normally, is executed using the supplied Executor, with this stage's result as the argument to the supplied function. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
fn Function: the function to use to compute the value of the returned CompletionStage
executor Executor: the executor to use for asynchronous execution
Returns
CompletionStage<U> the new CompletionStage

thenCombine

Added in API level 24
CompletionStage<V> thenCombine (CompletionStage<? extends U> other, 
                BiFunction<? super T, ? super U, ? extends V> fn)

Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed with the two results as arguments to the supplied function. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
fn BiFunction: the function to use to compute the value of the returned CompletionStage
Returns
CompletionStage<V> the new CompletionStage

thenCombineAsync

Added in API level 24
CompletionStage<V> thenCombineAsync (CompletionStage<? extends U> other, 
                BiFunction<? super T, ? super U, ? extends V> fn)

Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed using this stage's default asynchronous execution facility, with the two results as arguments to the supplied function. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
fn BiFunction: the function to use to compute the value of the returned CompletionStage
Returns
CompletionStage<V> the new CompletionStage

thenCombineAsync

Added in API level 24
CompletionStage<V> thenCombineAsync (CompletionStage<? extends U> other, 
                BiFunction<? super T, ? super U, ? extends V> fn, 
                Executor executor)

Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed using the supplied executor, with the two results as arguments to the supplied function. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
other CompletionStage: the other CompletionStage
fn BiFunction: the function to use to compute the value of the returned CompletionStage
executor Executor: the executor to use for asynchronous execution
Returns
CompletionStage<V> the new CompletionStage

thenCompose

Added in API level 24
CompletionStage<U> thenCompose (Function<? super T, ? extends CompletionStage<U>> fn)

Returns a new CompletionStage that is completed with the same value as the CompletionStage returned by the given function.

When this stage completes normally, the given function is invoked with this stage's result as the argument, returning another CompletionStage. When that stage completes normally, the CompletionStage returned by this method is completed with the same value.

To ensure progress, the supplied function must arrange eventual completion of its result.

This method is analogous to Optional.flatMap and Stream.flatMap.

See the CompletionStage documentation for rules covering exceptional completion.

Parameters
fn Function: the function to use to compute another CompletionStage
Returns
CompletionStage<U> the new CompletionStage

thenComposeAsync

Added in API level 24
CompletionStage<U> thenComposeAsync (Function<? super T, ? extends CompletionStage<U>> fn, 
                Executor executor)

Returns a new CompletionStage that is completed with the same value as the CompletionStage returned by the given function, executed using the supplied Executor.

When this stage completes normally, the given function is invoked with this stage's result as the argument, returning another CompletionStage. When that stage completes normally, the CompletionStage returned by this method is completed with the same value.

To ensure progress, the supplied function must arrange eventual completion of its result.

See the CompletionStage documentation for rules covering exceptional completion.

Parameters
fn Function: the function to use to compute another CompletionStage
executor Executor: the executor to use for asynchronous execution
Returns
CompletionStage<U> the new CompletionStage

thenComposeAsync

Added in API level 24
CompletionStage<U> thenComposeAsync (Function<? super T, ? extends CompletionStage<U>> fn)

Returns a new CompletionStage that is completed with the same value as the CompletionStage returned by the given function, executed using this stage's default asynchronous execution facility.

When this stage completes normally, the given function is invoked with this stage's result as the argument, returning another CompletionStage. When that stage completes normally, the CompletionStage returned by this method is completed with the same value.

To ensure progress, the supplied function must arrange eventual completion of its result.

See the CompletionStage documentation for rules covering exceptional completion.

Parameters
fn Function: the function to use to compute another CompletionStage
Returns
CompletionStage<U> the new CompletionStage

thenRun

Added in API level 24
CompletionStage<Void> thenRun (Runnable action)

Returns a new CompletionStage that, when this stage completes normally, executes the given action. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
action Runnable: the action to perform before completing the returned CompletionStage
Returns
CompletionStage<Void> the new CompletionStage

thenRunAsync

Added in API level 24
CompletionStage<Void> thenRunAsync (Runnable action, 
                Executor executor)

Returns a new CompletionStage that, when this stage completes normally, executes the given action using the supplied Executor. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
action Runnable: the action to perform before completing the returned CompletionStage
executor Executor: the executor to use for asynchronous execution
Returns
CompletionStage<Void> the new CompletionStage

thenRunAsync

Added in API level 24
CompletionStage<Void> thenRunAsync (Runnable action)

Returns a new CompletionStage that, when this stage completes normally, executes the given action using this stage's default asynchronous execution facility. See the CompletionStage documentation for rules covering exceptional completion.

Parameters
action Runnable: the action to perform before completing the returned CompletionStage
Returns
CompletionStage<Void> the new CompletionStage

toCompletableFuture

Added in API level 24
CompletableFuture<T> toCompletableFuture ()

Returns a CompletableFuture maintaining the same completion properties as this stage. If this stage is already a CompletableFuture, this method may return this stage itself. Otherwise, invocation of this method may be equivalent in effect to thenApply(x -> x), but returning an instance of type CompletableFuture. A CompletionStage implementation that does not choose to interoperate with others may throw UnsupportedOperationException.

Returns
CompletableFuture<T> the CompletableFuture
Throws
UnsupportedOperationException if this implementation does not interoperate with CompletableFuture

whenComplete

Added in API level 24
CompletionStage<T> whenComplete (BiConsumer<? super T, ? super Throwable> action)

Returns a new CompletionStage with the same result or exception as this stage, that executes the given action when this stage completes.

When this stage is complete, the given action is invoked with the result (or null if none) and the exception (or null if none) of this stage as arguments. The returned stage is completed when the action returns.

Unlike method handle, this method is not designed to translate completion outcomes, so the supplied action should not throw an exception. However, if it does, the following rules apply: if this stage completed normally but the supplied action throws an exception, then the returned stage completes exceptionally with the supplied action's exception. Or, if this stage completed exceptionally and the supplied action throws an exception, then the returned stage completes exceptionally with this stage's exception.

Parameters
action BiConsumer: the action to perform
Returns
CompletionStage<T> the new CompletionStage

whenCompleteAsync

Added in API level 24
CompletionStage<T> whenCompleteAsync (BiConsumer<? super T, ? super Throwable> action)

Returns a new CompletionStage with the same result or exception as this stage, that executes the given action using this stage's default asynchronous execution facility when this stage completes.

When this stage is complete, the given action is invoked with the result (or null if none) and the exception (or null if none) of this stage as arguments. The returned stage is completed when the action returns.

Unlike method handleAsync, this method is not designed to translate completion outcomes, so the supplied action should not throw an exception. However, if it does, the following rules apply: If this stage completed normally but the supplied action throws an exception, then the returned stage completes exceptionally with the supplied action's exception. Or, if this stage completed exceptionally and the supplied action throws an exception, then the returned stage completes exceptionally with this stage's exception.

Parameters
action BiConsumer: the action to perform
Returns
CompletionStage<T> the new CompletionStage

whenCompleteAsync

Added in API level 24
CompletionStage<T> whenCompleteAsync (BiConsumer<? super T, ? super Throwable> action, 
                Executor executor)

Returns a new CompletionStage with the same result or exception as this stage, that executes the given action using the supplied Executor when this stage completes.

When this stage is complete, the given action is invoked with the result (or null if none) and the exception (or null if none) of this stage as arguments. The returned stage is completed when the action returns.

Unlike method handleAsync, this method is not designed to translate completion outcomes, so the supplied action should not throw an exception. However, if it does, the following rules apply: If this stage completed normally but the supplied action throws an exception, then the returned stage completes exceptionally with the supplied action's exception. Or, if this stage completed exceptionally and the supplied action throws an exception, then the returned stage completes exceptionally with this stage's exception.

Parameters
action BiConsumer: the action to perform
executor Executor: the executor to use for asynchronous execution
Returns
CompletionStage<T> the new CompletionStage

Hooray!