title: Function returns 'Undefined'
body: <p>Not sure what am I missing, can someone please explain why the result is always undefined?</p>
  
  <p>In the code below I'm sending a get request and I want to return the results back to the caller.</p>
  
  <pre><code>var output = getUserName('the input');
  console.log(output) // undefined &lt;------
  
  function getUserName(userInput) {
      var result = '';
      const https = require('https');
      var token = 'xoxp-fdfs-fdsf';
      var user = userInput.split(" ");
      var url = 'https://slack.com/api/users.info?token=' + token +'&amp;user=' + user;
  
      https.get(url, (resp) =&gt; {
        let data = '';
  
        // A chunk of data has been recieved.
        resp.on('data', (chunk) =&gt; {
          data += chunk;
        });
  
        // The whole response has been received. Print out the result.
        resp.on('end', () =&gt; {
  
          data = JSON.parse(data);
          console.log('data.user.name: '+data.user.name); //output: John Doe (as expected)
          result = data.user.name;
          return result;
  
        });
  
      }).on("error", (err) =&gt; {
        console.log("Error: " + err.message);
        return "Error: " + err.message;
      });   
  
      return result; 
  }
  </code></pre>