title: Accessing variables inside a Javascript promise chain
body: <p>I am using a chained promise in JavaScript (I think). There is a then() function in the chain. I want to access the variable inside the promise, or somehow return the variable via my HTTP response object. </p>
  
  <pre><code>var getTitle = function(response)
  {
      console.log("Starting getTitle. response: " + response); //this works
  
      var horseman = new Horseman();           // object for headless browser
  
      horseman
          .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0")
          .open('http://www.google.com/ncr')
          .type('input[name="q"]', 'github')
          .click("button:contains('Google Search')")  
          .keyboardEvent("keypress",16777221) // press Enter
          .waitForSelector("div.g")        
          .title()            // gets the title of the page
          .then(function(t) {
              console.log("title: " + t);     // this works
          })
          .close();
  
      console.log("title outside: " + t);     // this gives 'undefined'
  
      return t;          // returns 'undefined'
  }
  </code></pre>
  
  <p>How can I extract the 't' variable? I also tried passing 'response' into the function like</p>
  
  <pre><code>.then(function(t, response) {
  </code></pre>
  
  <p>But when I log 'response', it is undefined. If I could pass in the response object somehow, that would also work. </p>
  
  <p>If I do</p>
  
  <pre><code>var test = horseman...
  </code></pre>
  
  <p>test becomes a promise object, but it doesn't contain the t variable. </p>