如何在javascript中获取回调函数内的结果?

时间:2013-02-13 14:15:34

标签: javascript node.js callback

如果我想将回调函数内部的结果写入响应,但是我无法访问函数中的res变量,而且我也无法访问函数外的结果。

那么如何在内外传递价值?

var http = require('http');
var url = require('url');

http.createServer
(
    function (req, res)
    {
        var output='';
        res.writeHead(200, {'Content-Type': 'text/plain'});


        //The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
        var options = {
          host: 'www.random.org',
          path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
        };

        callback = function(response) {
          var str = '';

          //another chunk of data has been recieved, so append it to `str`
          response.on('data', function (chunk) {
            str += chunk;
          });

          //the whole response has been recieved, so we just print it out here
          response.on('end', function () {
            console.log(str);

            //output+=str; //get result
          });
        }

        http.request(options, callback).end();


        //output+=str; //get result
        res.end(output);
    }
).listen(80, '127.0.0.1');

VER2

var http = require('http');
var url = require('url');

http.createServer
(
    function (req, res)
    {
        var output='';
        res.writeHead(200, {'Content-Type': 'text/plain'});


        //The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
        var options = {
          host: 'www.random.org',
          path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
        };

        callback = function(response) {
          var str = '';

          //another chunk of data has been recieved, so append it to `str`
          response.on('data', function (chunk) {
            str += chunk;
          });

          //the whole response has been recieved, so we just print it out here
          response.on('end', function () {
            res.write('inside');
          });
        }

        http.request(options, callback).end();

        res.write('outside');
        res.end(output);
    }
).listen(80, '127.0.0.1');

VER3

var http = require('http');
var url = require('url');

http.createServer
(
    function (req, res)
    {
        res.writeHead(200, {'Content-Type': 'text/plain'});


        //The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
        var options = {
          host: 'www.random.org',
          path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
        };

        callback = function(res) {
          var str = '';

          //another chunk of data has been recieved, so append it to `str`
          res.on('data', function (chunk) {
            str += chunk;
          });

          //the whole response has been recieved, so we just print it out here
          res.on('end', function () {
            res.write('inside');
            //or
            this.write('inside');
          });
        }

        http.request(options, callback).end();


        res.write('outside');
        res.end();
    }
).listen(80, '127.0.0.1');

ver4

var http = require('http');
var url = require('url');

http.createServer
(
    function (req, res)
    {
        res.writeHead(200, {'Content-Type': 'text/plain'});


        //The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
        var options = {
          host: 'www.random.org',
          path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
        };

        callback = function(response) {
          var str = '';

          //another chunk of data has been recieved, so append it to `str`
          response.on('data', function (chunk) {
            str += chunk;
          });

          //the whole response has been recieved, so we just print it out here
          response.on('end', function () {
            res.write('inside');
          });
        }

        http.request(options, callback);


        res.write('outside');
        res.end();
    }
).listen(80, '127.0.0.1');

1 个答案:

答案 0 :(得分:0)

你做不到。设置回调的函数将在回调执行之前完成并返回。如果没有,则不需要回调。

在回调本身做任何你需要做的工作,而不是在父函数中做。

  

我无法访问函数

中的res变量

你应该能够。它仍在范围内。

(虽然如果在回调运行之前在其上调用end(),则应该预期破损。)