javascript - How to pass data from the "then" methods in CasperJS? - Stack Overflow

It's mon to have multiple then methods when working with CasperJS. The following is an example:cas

It's mon to have multiple then methods when working with CasperJS. The following is an example:

casper.then(function(){
    var a = "test";
    // ...
})

casper.then(function(){
    // how to use the variable a in the first "then"
})

My question is, what's the mon way to pass values from former thens to following thens? For the aforementioned example, how to use a in the second then?

It's mon to have multiple then methods when working with CasperJS. The following is an example:

casper.then(function(){
    var a = "test";
    // ...
})

casper.then(function(){
    // how to use the variable a in the first "then"
})

My question is, what's the mon way to pass values from former thens to following thens? For the aforementioned example, how to use a in the second then?

Share Improve this question edited Oct 23, 2014 at 22:39 Artjom B. 62k26 gold badges135 silver badges230 bronze badges asked Oct 23, 2014 at 22:20 Just a learnerJust a learner 28.7k53 gold badges166 silver badges248 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

There are many way, but the easiest would be to use global variables. If you don't want to clutter your scripts with global variables (which should not be of the same concern as global variables in the browser, because there you could have different libraries), you can use IIFEs to reduce the scope.

casper.start(url);
(function(casper){
    var a;
    casper.then(function(){
        // set a
    }).then(function(){
        // use a
    });
})(casper);
casper.run();

Another version of the global one is to add those variables to the casper object.

Probably the cleanest solution would be to nest those blocks that need the variable. You have to keep in mind that a synchronous function call cannot e after an asynchronous one (those are all wait* and then* step functions). Scheduled steps are executed after the current stap has ended:

casper.start(url).then(function(){
    var a; // set a somehow
    this.then(function(){
        // use a
    });
}).then(function(){
    // don't use a
}).run();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744580381a4581999.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信