Richard Tjerngren created KAFKA-6900:
----------------------------------------

             Summary: Add thenCompose to KafkaFuture
                 Key: KAFKA-6900
                 URL: https://issues.apache.org/jira/browse/KAFKA-6900
             Project: Kafka
          Issue Type: Improvement
          Components: clients
    Affects Versions: 1.1.0
            Reporter: Richard Tjerngren


KafkaFuture supports Future chaining via the thenApply method just like 
CompletableFuture, however, thenApply is not intended to be used for lambdas 
that in turn return a future:

 
{code:java}
KafkaFutureImpl<String> future = new KafkaFutureImpl<>();
KafkaFuture<KafkaFuture<String>> nestedFuture = future.thenApply(result -> 
methodThatReturnsFuture(result));
{code}
Completable future has a method called thenCompose 
[javadoc|https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/CompletionStage.html#thenCompose(java.util.function.Function)]

The would be:
{code:java}
public KafkaFuture<R> thenCompose(Function<T, KafkaFuture<T> func);{code}
So the above example would look like this:
{code:java}
KafkaFutureImpl<String> future = new KafkaFutureImpl<>();
KafkaFuture<String> nestedFuture = future.thenCompose(result -> 
methodThatReturnsFuture(result));
{code}
This would enable developers to chain asynchronous calls in a more natural way 
and it also makes KafkaFuture behave more similar to Javas CompletableFuture 
and Javascripts Promise

 



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to