I am having a tough time figuring out something that should be simple. I am using the Vertx reactive library implementing a method that will return a Future and have an inline block of code that does the async processing and then when completed will use the promise to notify the returned Future of completion. The code below explains my question better.
/**
* Method that returns a Future<String> without blocking and writing code
* in the method that will be executed async, can't figure it out!
* @return
*/
public Future<String> asynMethod() {
// I create a promise to signal completion of future
// i create a future from the promise that i return
Promise<String> promise = Promise.promise();
Future<String> future = promise.future();
// now i want to write a block of code that will be async
// so that the future is returned right away.
// what do i wrap this block of code in to make it async?
// this is what I am trying to do but don't know how!
//Vertx vertx = Vertx.vertx();
// vertx.runAsync {
// String dbResult = queryDatabase();
// promise.completed(dbResult)
//}
return future;
}
There's no such method as Vertx.runAsync as far as I know. If you have a method defined as
Future<String> queryDatabase(), then the simplest version of your method might look like:or something like this:
I suggest having a good look at: