javascript - Need to run for loop and WaitFor in sync CasperJS -
i have code below
var casper = require('casper').create(); casper.on('remote.message', function (msg) { this.echo(msg); }); casper.start( << url >> , function () { this.echo(this.gettitle()); }); var resultobj = []; casper.thenclick("#addtocart").then(function () { // scrape else casper.options.waittimeout = 100000; var objectone = this.evaluate(somefunction, << variables >> ); //above function returns object casper.each(objectone, function (self, obj) { var anotherobject = this.evaluate(anotherfunction, << variables >> ); self.waitfor(function check() { var result = this.evaluate(thirdfunction, obj); if (result != 'no') { resultobj.push(result); } // result = 'yes'; return result != 'no'; this.evaluate(function () {}); }, function then() { console.log('done') }); }); }); casper.run(function () { this.exit(); });
it contains loop (.each) followed wait for. problem facing loop gets executed , waitfor gets executed. how can achieve them in sync?
looks want use casper.eachthen()
instead of casper.each()
.
warning: need @ least, casperjs 1.1-beta1 in order run this.
i wasn't able out of code, looks may want change few of casper.evaluate()
casper.thenevaluate()
i added // ---
around code modified below. hope helps.
var casper = require('casper').create(); casper.on('remote.message', function (msg) { this.echo(msg); }); casper.start( << url >> , function () { this.echo(this.gettitle()); }); var resultobj = []; casper.thenclick("#addtocart").then(function () { // scrape else casper.options.waittimeout = 100000; var objectone = this.evaluate(somefunction, << variables >> ); //above function returns object // --- casper.eachthen(objectone, function (response) { // --- var anotherobject = this.evaluate(anotherfunction, << variables >> ); this.waitfor(function check() { // --- var result = this.evaluate(thirdfunction, response.data); // --- if (result != 'no') { resultobj.push(result); } // result = 'yes'; return result != 'no'; this.evaluate(function () {}); }, function then() { console.log('done') }); }); }); casper.run(function () { this.exit(); });