title: Use result of HTTPS GET request [Node.js]
body: <p>I'm stuck with a small problem I think, but I can't find a way to solve it.
  I want to load a remote JSON in a variable, using Node.js HTTPS GET request. It's a success, but I can't use it anywhere else in my code. My function is the following (from Node.js doc):</p>
  
  <pre><code>function getMyFile() {
    var https = require('https');
  
    https.get('URL_I_am_targeting/file.json', (res) =&gt; {
      var { statusCode } = res;
      var contentType = res.headers['content-type'];
  
      let error;
  
      if (statusCode !== 200) {
        error = new Error('Request Failed.\n' +
                          `Status Code: ${statusCode}`);
      } else if (!/^application\/json/.test(contentType)) {
        error = new Error('Invalid content-type.\n' +
                          `Expected application/json but received ${contentType}`);
      }
  
      if (error) {
        console.error(error.message);
        // consume response data to free up memory
        res.resume();
      }
  
      res.setEncoding('utf8');
      let rawData = '';
  
      res.on('data', (chunk) =&gt; {
        rawData += chunk;
      });
  
      res.on('end', () =&gt; {
        try {
          const parsedData = JSON.parse(rawData);
          console.log(parsedData);
        } catch (e) {
          console.error(e.message);
        }
      });
    }).on('error', (e) =&gt; {
      console.error(`Got error: ${e.message}`);
    });
  }
  </code></pre>
  
  <p>The request works fine, as I am able to log my .json in the console correctly. But my problem is that I can't use the constant "parsedData" anywhere else in my code. I tried to write my</p>
  
  <pre><code>return parsedData;
  </code></pre>
  
  <p>at several places in my function, but when I use the function in my code and try f. e.</p>
  
  <pre><code>var fileContent = getMyFile();
  console.log(fileContent);
  </code></pre>
  
  <p>fileContent is undefined. It's like parsedData can't go out of the request. There is no error logged, the request goes fine, but I just can't use the content out of it.</p>
  
  <p>I am not a Javascript professional, and it's probably something I get wrong with returning value from this request.</p>
  
  <p>If somebody knows where I'm wrong and what could do the trick, it will be greatly appreciated !
  Thanks !</p>