0
var casper = require('casper').create();
casper.start();


casper.then(function(){
    for(var i=1;i<3;i++){
        this.repeat(3,function(){
            this.echo("loop iteration!");
        });
        this.echo("hello"); 
    }
});

casper.run()

Output:

hello hello loop iteration! loop iteration! loop iteration! loop iteration! loop iteration! loop iteration!

Why does "hello" print first?

How to write loop as synchronize loop?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Possible duplicate of [What must be wrapped in then() statements in CasperJS? How to determine execution order of sync/async functions?](http://stackoverflow.com/questions/30105017/what-must-be-wrapped-in-then-statements-in-casperjs-how-to-determine-executio) – Artjom B. Sep 19 '16 at 17:52

1 Answers1

0

Artjom is correct. You have to use then(). Something like:

for(var i=1;i<3;i++){
        this.repeat(3,function(){
            this.echo("loop iteration!");
        });
        this.then(function(){
                this.echo("hello");
        });
}
Lucamjj
  • 53
  • 6