Javascript 如何在casperjs中创建同步调用

Javascript 如何在casperjs中创建同步调用,javascript,phantomjs,casperjs,Javascript,Phantomjs,Casperjs,我有下面这样的代码 function main() { casper.start(); status = "A"; while(true){ if(status == "C"){ casper.then(function(){ status = "stop"; // the value status is stop in second loop }); }else{ casper.then(function(){

我有下面这样的代码

function main() {
casper.start();
status = "A";
while(true){
   if(status == "C"){
       casper.then(function(){
           status = "stop";  // the value status is stop  in second loop
       });
   }else{
       casper.then(function(){
           status = "C";  // modify status value in first loop
       });
   }
   if(status == "stop"){
       console.log("stop");
       break; // the loop should  break in second loop
   }
   console.log("run");
}
casper.run(function () {
    this.exit();
});
}

以上的输出应为

  C
  run
  stop
但实际上,这个程序陷入了一个无限循环。输出刚刚运行。 如何才能使casper.then在下一条语句后同步执行?
我这样做只是为了在状态机中使用casper。casper.then需要修改全局变量的值。但是现在我被上面的问题弄糊涂了。

您必须使用casper.then,下面是casperjs中的循环示例:谢谢,dashmelch