Wednesday 15 May 2013

javascript - How to update the URL in "then*" function? -



javascript - How to update the URL in "then*" function? -

i have below code snippet.

var admin_url = "http://www.sampledomain.com/"; var casper = require('casper').create(); casper.start(); casper.thenopen( "http://www.test.com", { method: "post", data: { param1: "some data" } }, function() { what_i_want = this.getpagecontent().split("[")[1].split("]")[0] admin_url += what_i_want; } ); casper.thenopen(admin_url, function() { this.echo(this.getcurrenturl()); }); casper.run();

i first post http://www.test.com, utilize returned data, seek modify admin_url, sec url need access. however, testing shows sec thenopen method still got old admin_url value , did not pick latest updated value (notice i've updated in first thenopen method). what's reason of issue , how prepare it?

when phone call thenopen admin_url passed value, thenopen executed asynchronously. can see way: add together steps queue , when phone call run, queue begins execute initial steps , accompanying data. need add together lastly thenopen step dynamically queue when admin_url complete:

casper.thenopen( "http://www.test.com", { method: "post", data: { param1: "some data" } }, function then() { whatiwant = this.getpagecontent().split("[")[1].split("]")[0] admin_url += video_id; this.thenopen(admin_url, function() { this.echo(this.getcurrenturl()); }); } ); casper.run();

there possiblity break thenopen sub-functions , utilize admin_url global variable:

var admin_url = "..."; casper.thenopen( "http://www.test.com", { method: "post", data: { param1: "some data" } }, function() { what_i_want = this.getpagecontent().split("[")[1].split("]")[0] admin_url += what_i_want; } ).then(function() { this.open(admin_url); }).then(function() { this.echo(this.getcurrenturl()); }).run();

it reduces nesting might thing based on preference.

javascript casperjs

No comments:

Post a Comment