title: Returning a value of a function with Promise object
body: <p>I am wring a <strong>getWebContent</strong> function that returns the content of a webpage using Promise (I am also using Request module).</p>
  
  <p>The way I'd like to use this function is <strong>var content = getWebContent()</strong>, so that <strong>content</strong> variable contains the data of the requested website. I started as follows:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var request = require('request')
  
  var getWebContent = function () {
      
      target = 'http://www.google.com';
      var result = null;
      var get = function (url) {
          return new Promise(function (resolve, reject) {
              function reqCallback(err, res, body) {
                  if (err) reject(err);
                  else resolve(body);
              };
              request(url, reqCallback);
          });
      };
  
      get(target).then(function (res) {
          result = res;
          console.log(res);
      });
      
      return result;
  };
  
  var goog = getWebContent();
  console.log(goog)</code></pre>
  </div>
  </div>
  </p>
  
  <p>However, this code does not work, because the function returns <strong>result</strong> variable, which is null, before the Promise object is resolved. Could you please let me know how I should fix my code so that it works as intended?</p>