1

I'm very green to node.js, I understand due to the asynchronus nature of callbacks the call back will not be returned before the variables str gets set.
I noticed that line 165 prints outs successfully, but 171 fails for the aforementioned reasons.

I've read several posts, but having trouble understanding how to re-write my code so the variable str contains the contents of the console.log in line 165. I tried experimenting with .binds{str:str}, but no luck. How do I obtain the contents of str outside the scope of the callback once its done?

154 var str;
155 var  callBack  = function(response) {
156 var str = '';
157
158  //another chunk of data has been recieved, so append it to `str`
159   response.on('data', function (chunk) {
160    str += chunk;
161   } );
162
163  //the whole response has been recieved, so we just print it out here
164  response.on('end', function () {
165  console.log("******************"+str+"*********************");
166   });                        
167 };  
168                       
169   http.request(url, callBack).end();                            
170   console.log(str);                              
171   console.log("outside of callback---------------------xxx"+str+"xxx-----------------------------");
ConfusedDeer
  • 3,335
  • 8
  • 44
  • 72
  • 1
    you call 186 before str is set, you need to wait for the response to end... in short, the answer to your question is "you don't"; you must move what needs to be done to the variable, not move the variable to what needs to be done... – dandavis Jul 16 '14 at 02:58
  • How do I wait for the response to end before setting the VAR? – ConfusedDeer Jul 16 '14 at 03:02
  • the end event of the response is a great place to run the code... – dandavis Jul 16 '14 at 03:03
  • This call back is done inside another API, so I'm requesting data from an API from within an API. The client calls my API, I then call another API, wait for the response and send the str variable back to the client. BTW: No I can't give access to the client to hit the API that I'm hitting directly the client has to go through my API. So When you state to write the code in response.on, I really can't because I have to send the str variable back in a res.end(str). – ConfusedDeer Jul 16 '14 at 03:08
  • 3
    there's no reason you can't do that inside the end event handler. – Kevin B Jul 16 '14 at 03:10
  • 1
    *"How do I obtain the contents of str outside the scope of the callback once its done?"* The scope the is not the problem. The problem is the "when it's done" part. You can only know that the callback was fired from inside the callback itself. Thus, any code that needs the data has to be inside or called from the callback. – Felix Kling Jul 16 '14 at 03:13
  • I'm going to do the res.end(str); in the response.on, so...response.on('end', function(){ res.end(str);}); – ConfusedDeer Jul 16 '14 at 03:15
  • It worked! Thank you...dandavis and kevin b. So obvious now. Please Answer Dan Davis or Kevin B, so I can mark as answered. – ConfusedDeer Jul 16 '14 at 03:21
  • 1
    async essentially forces you to do something fancy called "dependency injection", but JS let's you do that easily by using closure and/or other callbacks that get passed data. in short, you can usually just about wrap about a few dozen chars around an existing piece of sync code (an anon callback) and have it work as coded before but run anync. that simplicity can eventually give way to nested complexity, but we have ways of handling that too, ex promises. – dandavis Jul 16 '14 at 04:42

0 Answers0