-1

I want to save the HTML of an extern site in a var. I have the following code:
program.js

var request = require('request');

var htmlcode = "Code isn't defined right now!";

var siteUrl = "http://fluxnetz.de"
request({

uri: siteUrl,
}, function(error, response, body){
var htmlcode = body;
console.log(htmlcode);
}
);
//console.log(htmlcode);

When I want to output htmlcode outside of the request function it outputs "Code isn't defined right now!" But I set htmlcode to body before. And when I call console.log inside the function I get the right output but if I run it again outside it wont work.

  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ben Fortune Jan 10 '17 at 15:33

2 Answers2

0

You need to understand how Javascript works. There are synchronous actions, and asynchronous actions.

synchronous means that code executes in order of lines

Asynchronous means that code does not execute in order of lines, but in a threaded way.

When you create a new "request", imaging node opening a new thread, relevant only to that reaquest's body. and when the request resolves itself, the thread joins back to the global program.

If you for example will do, instead of console.log(htmlcode); which is synchronous, an asynchronous action which is resolved later than the request, it would work, because the request's thread already joined.

The most naive, often working often not example would be to:

setTimeout(() => console.log(htmlcode), 10000)

Which waits 10 seconds, then prints. plenty of time for the request to finish.


The correct way, would be to create a function, called lets say "afterRequest" and call that function from the request resolve callback

Amit
  • 5,924
  • 7
  • 46
  • 94
  • That's true but it notes the issue here. the issue is that he set a local variable from with in he's callback – eran otzap Jan 10 '17 at 15:38
  • @eranotzap well. both are problems in this case. the asynchronous call and the local var – Amit Jan 10 '17 at 15:41
  • I agree he would actually come across you problem after solving that one. didn't get the 'let' comment below.. – eran otzap Jan 10 '17 at 15:42
-1

The reason is because you've deifned another local variable with in your callback

  var request = require('request');
  var htmlcode = "Code isn't defined right now!";

  var siteUrl = "http://fluxnetz.de"
  request({ uri: siteUrl, 
             function(error, response, body){
                 //var htmlcode = body; // Remove to var and you will address to global variable you wanted.
                 htmlcode = body;  
                 console.log(htmlcode);
              }
          });
eran otzap
  • 12,293
  • 20
  • 84
  • 139
  • @Amit are you saying that the new deceleration overrides the global one ? – eran otzap Jan 10 '17 at 15:41
  • I thought so, at first glance, then I saw I was terribly mistaken. That is not what 'let' solves, let solves similar cases in different scopes though. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/let includes some examples – Amit Jan 10 '17 at 15:44