BigBug
BigBug

Reputation: 6290

Get Result from CompositeFuture Vertx

I've recently learned about composite futures. I understand that i can have a list of Futures and when they are all completed, the compositeFuture handler will be called:

CompositeFuture.all(futures).onComplete(compositeAsync -> {
    if (compositeAsync.succeeded()){
        Future<CompositeFuture> cf = compositeAsync.result();
    } else {
        // at least 1 future failed
    }
});

My question is about this line: Future<CompositeFuture> cf = compositeAsync.result();

The futures that i passed into the compositeFuture is of type List<Future<Buffer>> however, the result() returns Future. How do i get the Future<Buffer> out of a Future<CompoiteFuture>?

I used the following example to pass in paramaterized futures into compositeFuture.all(futures);

interface MyCompositeFuture extends CompositeFuture {

    // This is what the regular does, just for example
    /*
    static CompositeFuture all(List<Future> futures) {
        return CompositeFutureImpl.all(futures.toArray(new Future[futures.size()]));
    }
    */

    static <T> CompositeFuture all(List<Future<T>> futures) {
        return CompositeFutureImpl.all(futures.toArray(new Future[futures.size()]));
    }
}

Source: Vert.x java List<Futures> parameterization

Upvotes: 1

Views: 1302

Answers (1)

Tom
Tom

Reputation: 3850

You should be getting the results, not the futures themselves:

CompositeFuture.all(futures).onComplete(compositeAsync -> {
    if (compositeAsync.succeeded()){
        CompositeFuture cf = compositeAsync.result();
        cf.list().forEach(futureResult -> {
            // do something with the result of each future
        });
    } else {
        // at least 1 future failed
    }
});

Note that CompositeFuture extends Future<CompositeFuture>, this is why your code works. But inside onComplete you should use it directly and not as a Future<CompositeFuture>

Upvotes: 5

Related Questions