简体   繁体   English

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

[英]How to get the result inside callback function in javascript?

If I want to write the result inside callback function to response, but I can't access the res variable in the function, and also I can't access the result outside the function. 如果我想将结果写入回调函数中以进行响应,但是我无法访问该函数中的res变量,也无法访问该函数之外的结果。

So how to pass value between inside and outside? 那么如何在内部和外部之间传递价值呢?

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 版本2

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 版本3

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 版本4

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');

You can't. 你不能 The function that sets up the callback will have finished and returned before the callback executes. 设置回调的函数将在回调执行之前完成并返回。 If it didn't, then the callback wouldn't be needed. 如果没有,则不需要回调。

Do whatever work you need to do in the callback itself, not in the parent function. 在回调本身(而不是父函数)中执行您需要做的所有工作。

I can't access the res variable in the function 我无法访问函数中的res变量

You should be able to. 你应该能够。 It is still in scope. 它仍然在范围内。

(Although if you call end() on it before the callback runs, then breakage should be expected). (尽管如果您在回调运行之前在其上调用end() ,则应该预期会损坏)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM