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.
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'
}
How can I extract the 't' variable? I also tried passing 'response' into the function like
.then(function(t, response) {
But when I log 'response', it is undefined. If I could pass in the response object somehow, that would also work.
If I do
var test = horseman...
test becomes a promise object, but it doesn't contain the t variable.
Try returning from within a .finally().
.finally(function(t){
return t;
});