我有一个代码如下
var casper = require('casper').create();
casper.on('remote.message', function (msg) {
this.echo(msg);
});
casper.start( << some url >> , function () {
this.echo(this.getTitle());
});
var resultObj = [];
casper.thenClick("#AddToCart").then(function () {
// scrape something 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();
});
它包含一个循环(.each),然后是等待。我面临的问题是循环被完全执行,然后 waitFor 被执行。我怎样才能使它们同步?