title: JS - HTTP Request using result in different function
body: <p>I have a GET request to get data (a string) from an URL using following code:</p>
  <pre><code>var https = require('https');
  let urldata;
  
  var options = {
      host: 'test.com',
      path: '/test.php'
  }
  var request = https.request(options, function (res) {
      let data = '';
      res.on('data', function (chunk) {
          data += chunk;
      });
      res.on('end', function () {
          urldata = data; 
          console.log(urldata);  //Returns the retrieved data from URL into console
      });
  });
  request.end();
  </code></pre>
  <p>Now I have a second function that needs the data that I get from the Request abvoe:</p>
  <pre><code>async execute(message, args, Discord, client) {
      message.author.send(urldata);
  }
  </code></pre>
  <p>The issue with that is &quot;urldata&quot; is &quot;undefined&quot; in the second function because the first one is async and didn't give a result yet. I've read that I will have to use &quot;callback&quot; or &quot;promise&quot; in order to use the data from the first function in my second one. I tried around with that but couldn't get a working result.</p>
  <p>It would be awesome if someone could help me out with recoding this to a callback or promise so the data from the first function could be used for the second function.</p>