Javascript 从casperjs waitFor()返回数据

Javascript 从casperjs waitFor()返回数据,javascript,casperjs,Javascript,Casperjs,我想知道waitFor的结果。考虑下面的代码: var success = false; casper.waitFor(function check(){ return isSuccess(); },function then(){ casper.echo("Great succees"); success = true; },function fail(){ casper.echo("Failure"); },2000); console.log("Did we

我想知道
waitFor
的结果。考虑下面的代码:

var success = false;
casper.waitFor(function check(){
    return isSuccess();
},function then(){
    casper.echo("Great succees");
    success = true;
},function fail(){
    casper.echo("Failure");
},2000);
console.log("Did we make it? "+success);
不幸的是,即使在执行
then()
时,全局
success
似乎超出范围,并且不会更新为
true
。我还考虑过让
waitFor
返回这个标志,但是这个函数似乎返回一个
casper
对象


想法

所有
wait*
函数以及所有
then*
函数都是阶跃函数。这意味着已计划执行传递的回调函数。它们本质上是异步的

var success = false;
casper.waitFor(check, function then(){
    casper.echo("Great success");
    success = true;
}, onTimeout, 2000);
casper.then(function(){
    // here: the waitFor functions are guaranteed to have run
    console.log("Did we make it? "+success);
});
当您在另一个步骤函数回调中调用
waitFor
(或任何其他步骤函数)时,这些步骤将被安排在当前步骤函数结束时执行。这意味着,如果调用异步
waitFor
,然后调用同步
console.log
,结果将不会就绪

casper.then(function(){
    var success = false;
    casper.waitFor(check, function then(){
        casper.echo("Great success");
        success = true;
    }, onTimeout, 2000);
    console.log("Did we make it? "+success); // here: the none of the waitFor functions are executed yet
});
对于全局情况也是如此,其中步骤不在其他步骤的内部。您可以做的是使
console.log
异步

var success = false;
casper.waitFor(check, function then(){
    casper.echo("Great success");
    success = true;
}, onTimeout, 2000);
casper.then(function(){
    // here: the waitFor functions are guaranteed to have run
    console.log("Did we make it? "+success);
});

谢谢你的回答!我知道这些函数是异步回调。事实上,在我运行和测试这项功能的100次中,我首先在控制台上获得了
“巨大的成功”
,然后才看到
“我们成功了吗?错误”
输出,所以我觉得这是范围的问题,因为时间似乎还可以。我会按照你建议的方式来实施。再次感谢