Search code examples
javascriptasynchronousphantomjssynchronouscasperjs

Need to run for loop and WaitFor in sync CasperJS


I have a code as below

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();
});

It contains a loop (.each) followed by wait for. The problem that I am facing is the loop gets executed completely and then waitFor gets executed. How can I achieve them to be in sync?


Solution

  • Looks like you will want to use casper.eachThen() instead of casper.each().

    Warning: you will need at the very least, CasperJS 1.1-beta1 in order to run this.

    I wasn't able to get much out of your code, but it looks like you may also want to change a few of your casper.evaluate() to casper.thenEvaluate()

    I added // --- around the code that I modified below. Hope it helps.

    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.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();
    });