futurevert.xcompositechain

VertX Multiple Futures returning a Future


I am trying to call 3 futures, wait for them to complete, process the results and then return all of this also as a Future. I cant seem to figure out how to "wrap" the 3 futures in a future to be returned so that it doesnt just return immediately. I am new to vertx.

public Future<MyObject> getProjectStatus(int projectId) {

      Future future = Future.future(ar -> {
        MyObject object = new Myobject();

        Future<x> f1 = callf1();
        Future<y> f2 = callf2();
        Future<z> f3 = callf3();

        CompositeFuture cf = CompositeFuture.all(f1, f2, f3);
        cf.onComplete(ar2 -> {
          //Check Succeeds
          if(ar2.succeeded()){
            System.out.println("Completed!");
            //DO FURTHER PROCESSING
          } else {
            System.out.println("Failure " + ar2.cause().getMessage());
          }
        }).map((object));

      });

      return future;

    }

Solution

  • 
    public Future<MyObject> getProjectStatus(int projectId) {
    
            MyObject object = new Myobject();
    
            Future<x> f1 = callf1();
            Future<y> f2 = callf2();
            Future<z> f3 = callf3();
    
            return CompositeFuture.all(f1, f2, f3).onComplete(ar2 -> {
              //Check Succeeds
              if(ar2.succeeded()){
                System.out.println("Completed!");
                //DO FURTHER PROCESSING
              } else {
                System.out.println("Failure " + ar2.cause().getMessage());
              }
            }).map(object);
    
        }
    

    or

    public Future<MyObject> getProjectStatus(int projectId) {
    
          Future future = Future.future(ar -> {
            MyObject object = new Myobject();
    
            Future<x> f1 = callf1();
            Future<y> f2 = callf2();
            Future<z> f3 = callf3();
    
            CompositeFuture cf = CompositeFuture.all(f1, f2, f3);
            cf.onComplete(ar2 -> {
              //Check Succeeds
              if(ar2.succeeded()){
                System.out.println("Completed!");
                //DO FURTHER PROCESSING
              } else {
                System.out.println("Failure " + ar2.cause().getMessage());
              }
              ar.complete(object);
            });
    
          });
    
          return future;
    
        }