title: How do I set a variable with what is returned from an async request?
body: <p>So, long of the short, I am building a bot with node.js. Part of what I want to do is use an API from a website and set parts of the JSON file that is returned to a variable (although in my example I use the entire return to simplify my explanation/question). I use request() to access the API and if something is returned, I set the variable to the returned JSON information.</p>
  
  <p>I know that the code is properly returning the JSON information, because I can console.log() the JSON file out with the variable. Key thing is that the console.log() of this variable has to be done within request(). If I try to console.log() outside the request() function, it simply logs "undefined".</p>
  
  <p>I have searched around and can tell this has something to do with this being an async call. While that is great to know, I have yet to find a way to work around this issue.</p>
  
  <p>Below is a simplified version of the request() code (with the website missing because I use a key to access the API). I would just like to access the variable outside the request so that I can use it for something else. </p>
  
  <p>Please note the commented out console.log() comments. Code here:</p>
  
  <pre><code>var parsedData;
  request("WEBSITE HERE", function(error, response, body) {
      if(error){
          console.log("SOMETHING WENT WRONG!");
          console.log(error);
      }else{
          if(response.statusCode==200){
              parsedData = JSON.parse(body);
              //console.log(parsedData); works here and prints JSON data.
          }
      }});
  //console.log(parsedData); here console.log() is undefined.
  </code></pre>
  
  <p>To the individual claiming this is a duplicate in the link, it is not. I am asking how to use a variable outside this function and a solution was not brought up in the link provided. On top of that, it isn't node.js, although it is Javascript. Overall, it shares a similarity with async calls, but it is not the same question. </p>