admin管理员组

文章数量:1418041

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('')
        .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 bees a promise object, but it doesn't contain the t variable.

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./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 bees a promise object, but it doesn't contain the t variable.

Share Improve this question asked Feb 5, 2016 at 15:47 user3320795user3320795 5532 gold badges7 silver badges18 bronze badges 2
  • I'm not sure what you are trying to acplish, You say that the title is rightly available inside the .then function. Are you trying to return that to the caller of the function? – Robert Moskal Commented Feb 5, 2016 at 16:02
  • Yes. Well, the goal is to have response and t accessible in the same scope. – user3320795 Commented Feb 5, 2016 at 19:23
Add a ment  | 

3 Answers 3

Reset to default 1

This did it:

var getTitle = function(response)
{
    console.log("Starting getTitle. response: " + response); //this works

    var horseman = new Horseman();           // object for headless browser

    var xyz = horseman
        .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0")
        .open('http://www.google./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
        .finally(function (t) {
            horseman.close();
            return t;
        });

    return xyz;         
}

In the calling function:

var abc = getTitle(response);
abc.then(function(abc) {
    console.log("abc: " + abc);
    response.writeHead(200, {'Content-Type': 'text/html'});
    response.end(abc); 

It didn't work if I put any more lines of code inside the finally block; I don't know why. I can probably remove the response parameter; need to test that.

Try returning from within a .finally().

    .finally(function(t){
        return t;
    }); 

You cann't return t from function getTitle, but you have 2 options here.

1.To do all you need with t inside callback function in then, or pass callback function as parameter

var getTitle = function(response, callback) {
...
.then(function(t) {
    callback(t);
});

2.To return promise from getTitle and use then to handle response.

return horseman;
...
var t = getTitle(response);
t.then(function(t){/* handle t */});

本文标签: methodsAccessing variables inside a Javascript promise chainStack Overflow