我需要把'return'casper.then'块,让父函数等待子函数完成执行
Do I need to put the 'return' statement inside a 'casper.then' block for the parent function to wait for the child function to complete execution?
示例1:
function abc(){
// code1 takes 5 secs to execute;
// code2 takes 1 sec to execute;
});
casper.then(function(){
abc();
casper.then(function(){
console.log("All statements inside abc functions have been executed");
});
});
我观察到casperjs在启动第一个语句后立即启动下一个语句,除非我们使用casper.then()
。因此,在上面的代码中,code2在没有等待code1完成执行的情况下被启动。
我的问题是,一旦code2启动,控件是否会返回到调用函数,或者它会等待函数abc() 完成执行所有语句。请注意,在调用abc()之后的调用函数中有一个casper.then()
。
如果它不等待code1完成,我可以这样做:示例2:
function abc(){
// code1 takes 5 secs to execute;
// code2 takes 1 sec to execute;
casper.then(function(){
return;
});
});
casper.then(function(){
abc();
casper.then(function(){
console.log("All statements inside abc functions have been executed");
});
});
我希望console.log()只在abc()中的所有语句完全执行后才执行。
这取决于code1
和code2
是什么。如果它是完全同步的代码,我怀疑,因为你问了这个问题,你可以使用then
之后:
function abc(){
// code1 takes 5 secs to execute;
// code2 takes 1 sec to execute;
});
casper.then(function(){
abc();
casper.then(function(){
console.log("All statements inside abc functions have been executed");
});
});
如果它不是同步的,则需要在complete回调(或其他可能命名的回调)中将一些全局变量设置为finish值。然后waitFor
它的执行:
var globalObj = {};
function abc(){
// code1 takes 5 secs to execute and sets globalObj.code1 = true
// code2 takes 1 sec to execute and sets globalObj.code2 = true
});
casper.then(function(){
abc();
// you can wait for the two async calls separately
casper.waitFor(function check(){
return (code1 in globalObj) && globalObj.code1;
}, null, null, 10000);
casper.waitFor(function check(){
return (code2 in globalObj) && globalObj.code2;
}, null, null, 10000);
// or it can even be combined
casper.waitFor(function check(){
return (code1 in globalObj) && globalObj.code1 && (code2 in globalObj) && globalObj.code2;
}, function then(){
console.log("All statements inside abc functions have been executed");
// you can nest more then or other step functions here,
// if you only want execution for a successful waitFor
}, null, 10000);
// this then is only executed when the waitFor stopped (either because of successful check or because of timeout)
casper.then(function(){
console.log("All statements inside abc functions have been executed");
});
});
顺便说一下,一个空的then
块什么也不做。这对等待没有任何帮助。你可以完全删除这个:
casper.then(function(){
return;
});
就我在文档中看到的而言,您应该将这些步骤声明为彼此相邻,而不是在彼此之间
所以,你应该使用:
casper.then(function() {
abc();
});
casper.then(function() {
console.log('all statements have been executed');
});
查看更多信息:http://docs.casperjs.org/en/latest/modules/casper.html#then
相关文章:
- JavaScript循环并等待函数
- 等待函数执行后再继续
- 嵌套的异步/等待函数
- 等待函数完成,然后将返回值分配给变量JS
- 如何在Node.js中等待函数完成后再继续
- 如何在JavaScript中创建一个等待函数,允许您在后台运行音频等内容
- 等待函数结束(在 async.series 中)无法按预期工作
- 等待函数 $.get 中的代码完成以继续
- Javascript等待函数结束,包括来自node.js的异步MYSQL查询
- 并行调用异步/等待函数
- Javascript 睡眠/延迟/等待函数
- 如何在 Node.js 中等待函数完成
- Javascript - 等待函数完成
- Javascript for loop不等待函数响应
- 等待函数完成对其他函数的调用
- 循环的Javascript,等待函数回调
- 如何使angular在继续之前等待函数返回值
- 如何让html在AngularJS控制器中等待函数
- Node.js强制等待函数完成
- azure异步javascript后端等待函数