如何在函数返回时使Node JS异步

时间:2017-02-13 11:04:19

标签: javascript node.js

以下函数调用Rest Web Service并获取一些值,但返回结果时不包含任何值。

var http = require(' http'); var options = {   主持人:' localhost',//这里只有域名http://localhost:8080/RestService/rest/message/getItems         //(没有http / http!)         港口:8080,         路径:' / RestService / rest / message / getItems',//如果需要,带有参数的其余网址         方法:' GET' //做GET };

function getService(){

http.request(options, function(res) {

  console.log('STATUS1: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
        return chunk;
  });
}).end();

}; console.log("返回值 - >" + getService());

1 个答案:

答案 0 :(得分:-1)

使用以下方式:

get_class()