Continuing the question from here
https://groups.google.com/d/msg/vertx-dev/p-NRTEnqDQE/hiMWMgxzBQAJThe result of an async operation is not available when the operation is started, only later in the handler, which is called when the operation is finished. This is a bit counter-intuitive, since the flow of the program is different than it looks in the source code and the variable definitions are available.
HttpClientOptions options = new HttpClientOptions().setKeepAlive(false);
options.setLogActivity(true);
options.setDefaultHost("delvmpllbbab10");
options.setDefaultPort(7003);
HttpClient client = vertx.createHttpClient(options);
String[] resp = {null};
client.request(HttpMethod.GET, "/rcsservices/homePage", response -> {
response.handler(buffer -> {
System.out.println("Received a part of the response body: " + buffer);
resp[0] = buffer.toString();
});
}).putHeader("content-type", "application/json").end(Json.encodePrettily(resp[0]));
System.out.println(resp[0]);
The handler lambda is executed much later than the code creating the request and the code after the method call, at this point the resp value is still null.
(btw, this also means that using it in the .end() method just creates a JSON null string)
If you consider of the execution of the different parts as thread-flows (for the lack of a better name, which isn't quite correct since it is not different threads, everything in the same context is executed in a single thread), the thread that creates the request continues with the code after the method immediately and the lambda is only a constant value at that point, it is run as another "thread" later.