vert.x icon indicating copy to clipboard operation
vert.x copied to clipboard

Add Future#compose(Supplier<U> supplier) method

Open aguibert opened this issue 5 years ago • 1 comments

Motivation:

When chaining operations with a Future object, sometimes operations only depend on the completion of the previous operation, and don't care about the result value of the previous operation.

For example, with vertx-sql-client to perform a simple "insert then select" secenario, we have to do this:

DB2Pool client = DB2Pool.pool(connectOptions, poolOptions);

// Note the "junk" variables
client.query("INSERT INTO Fortune (message) VALUES ('some data')").execute()
  .flatMap(junk -> client.query("SELECT * FROM Fortune WHERE id=23").execute())
  .onComplete(this::printResults)
  .flatMap(junk -> client.query("SELECT * FROM Fortune WHERE id=24").execute())
  .onComplete(this::printResults)
  .onComplete(junk -> {
    System.out.println("All done");
    client.close();
  });

After this new compose function is added, the above code reads a bit nicer:

DB2Pool client = DB2Pool.pool(connectOptions, poolOptions);

client.query("INSERT INTO Fortune (message) VALUES ('some data')").execute()
  .flatMap(() -> client.query("SELECT * FROM Fortune WHERE id=23").execute())
  .onComplete(this::printResults)
  .flatMap(() -> client.query("SELECT * FROM Fortune WHERE id=24").execute())
  .onComplete(this::printResults)
  .onComplete(() -> {
    System.out.println("All done");
    client.close();
  });

aguibert avatar Apr 02 '20 17:04 aguibert

I think we need support of Supplier in codegen and other languages before so it can be translated.

vietj avatar Apr 02 '20 18:04 vietj