这实际上是一个关于Javascript范围和函数的问题,所以我很抱歉,因为我是JS的新手!从Javascript函数调用返回值
我有一段javascript代码,连接创建一个HTTPClient连接到Vertx的REST服务 - 这一切都没问题。
我的问题是,我需要进入“状态”和“数据”的代码从这个调用的“client.request”外返回
// Create the HTTP Client
var client = vertx.createHttpClient().host(host).port(ssl_port).ssl(true);
var request = client.request("POST", "/api/en/v1.0/authentication/token/new" + "?login_id=" + login_id + "&api_key=" + api_key, function(resp) {
//console.log("Got a response: " + resp.statusCode());
resp.bodyHandler(function(body) {
// Parse the JSON string into a new object.
var response = JSON.parse(body);
console.log("status : " + response.status);
console.log("data : " + response.data);
})
});
request.end();
所以到这里我想使用/访问“状态”和“数据”。在另一种语言中,我只是将它们作为对象返回到调用堆栈中。
@Matt:我正在考虑将标题(和答案)更改为通用,针对异步函数调用。 –
@FelixKling使它发生':)' –
有关JavaScript作用域的详细说明:http://stackoverflow.com/questions/500431/javascript-variable-scope –