methods - Accessing variables inside a Javascript promise chain -
i using chained promise in javascript (i think). there then() function in chain. want access variable inside promise, or somehow return variable via http response object.
var gettitle = function(response) { console.log("starting gettitle. response: " + response); //this works var horseman = new horseman(); // object 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 title of page .then(function(t) { console.log("title: " + t); // works }) .close(); console.log("title outside: " + t); // gives 'undefined' return t; // returns 'undefined' }
how can extract 't' variable? tried passing 'response' function like
.then(function(t, response) {
but when log 'response', undefined. if pass in response object somehow, work.
if do
var test = horseman...
test becomes promise object, doesn't contain t variable.
try returning within .finally().
.finally(function(t){ return t; });
Comments
Post a Comment