FAQ
My situation is like this, I have a server like this and inside I am
calling another callback function which gets multiple values:

     var http = require('http');
     var server = http.createServer(req,resp) {
          var finalVal = "";

          consumer.on('message',function(message) {
              finalVal = message;
              console.log(finalVal);
          });

          resp.end(finalVal);
     });

My finalVal should display all the multiple values it fetches and send it
as a response, but problem is it's sending only first value where as
console.log displays all the values. I do understand that by the time
consumer.on ends response would have committed. Can someone please help me
how to handle this scenario since I'm very new to Node.js ? Currently due
to heavy deadlines I don't have time to read full information about
callbacks. But defnitely I would take time to learn about callbacks.

Here `consumer.on` calls multiple times till it fetches all the data from
backend, I need to send all those data in a final response. I am using
`node-kafka` to consume to kafka messages.

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/9cf99828-576a-4327-b700-6fedb62c7785%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Greelgorke at Aug 13, 2014 at 9:12 pm
    var http = require('http');
         var server = http.createServer(req,resp) {
              var finalVal = "";

              consumer.on('message',function(message) {
                  finalVal = message;
                  console.log(finalVal);
                  if(isRealyTheFinalMessage())
                       resp.end(finalVal);
              });


         });


    Am Mittwoch, 13. August 2014 04:55:28 UTC+2 schrieb Pradeep Simha:
    My situation is like this, I have a server like this and inside I am
    calling another callback function which gets multiple values:

    var http = require('http');
    var server = http.createServer(req,resp) {
    var finalVal = "";

    consumer.on('message',function(message) {
    finalVal = message;
    console.log(finalVal);
    });

    resp.end(finalVal);
    });

    My finalVal should display all the multiple values it fetches and send it
    as a response, but problem is it's sending only first value where as
    console.log displays all the values. I do understand that by the time
    consumer.on ends response would have committed. Can someone please help me
    how to handle this scenario since I'm very new to Node.js ? Currently due
    to heavy deadlines I don't have time to read full information about
    callbacks. But defnitely I would take time to learn about callbacks.

    Here `consumer.on` calls multiple times till it fetches all the data from
    backend, I need to send all those data in a final response. I am using
    `node-kafka` to consume to kafka messages.
    --
    Job board: http://jobs.nodejs.org/
    New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
    Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    ---
    You received this message because you are subscribed to the Google Groups "nodejs" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.
    To post to this group, send email to nodejs@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/fb3e9575-aba4-46d7-a8c3-6a073c82338b%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • // ravi at Aug 13, 2014 at 9:12 pm

    On Aug 12, 2014, at 10:55 PM, Pradeep Simha wrote:
    My situation is like this, I have a server like this and inside I am calling another callback function which gets multiple values:

    var http = require('http');
    var server = http.createServer(req,resp) {
    var finalVal = "";

    consumer.on('message',function(message) {
    finalVal = message;
    console.log(finalVal);
    });

    resp.end(finalVal);
    });
    Well, you can move the resp.end() inside the function(message) { } block. But if consumer.on() can fire multiple times and you need to accumulate the messages, how do you know when the data is complete? You will need to add a check for that.

      --ravi


    My finalVal should display all the multiple values it fetches and send it as a response, but problem is it's sending only first value where as console.log displays all the values. I do understand that by the time consumer.on ends response would have committed. Can someone please help me how to handle this scenario since I'm very new to Node.js ? Currently due to heavy deadlines I don't have time to read full information about callbacks. But defnitely I would take time to learn about callbacks.

    Here `consumer.on` calls multiple times till it fetches all the data from backend, I need to send all those data in a final response. I am using `node-kafka` to consume to kafka messages.
    --
    Job board: http://jobs.nodejs.org/
    New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
    Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    ---
    You received this message because you are subscribed to the Google Groups "nodejs" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.
    To post to this group, send email to nodejs@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/0AEDAF58-E503-4A12-8BED-5D957025017F%40g8o.net.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupnodejs @
categoriesnodejs
postedAug 13, '14 at 6:56a
activeAug 13, '14 at 9:12p
posts3
users3
websitenodejs.org
irc#node.js

People

Translate

site design / logo © 2021 Grokbase