CasperJs然后()等待上一个函数中发出的事件? [英] Does CasperJs then() wait on emitted events in the previous function?
问题描述
$ b
casper.on 'foo',function(){
this.wait(60000);
this.echo('foo');
});
casper.start('http://www.stackoverflow.com',function(){
this.echo('start');
这个.emit('foo');
});
casper.then(function(){
this.echo('done');
});
casper.run();
我知道then()将等待检查 3个标志:pendingWait,loadInProgress和navigationRequested。打印出来的调用堆栈显示在start()函数中的emit调用,所以start()不会被完成,直到事件完成为止?即然后()等待直到事件完成
我用等待60秒测试了这个,我得到了输出:
开始
foo
done
虽然我不确定超过某个超时是否会触发下一个()。
要记住哪些功能是异步执行的。在您的情况下,输出的前两行在页面加载时立即打印,并在60秒后打印 done
。
需要注意的事项:
- 所有
然后*
和wait *
函数将在队列中插入一个步骤,但本身就是异步的。 -
casper.emit
将寻找注册的事件处理程序并立即执行(非异步)。 -
casper.echo
将打印一些内容立即(非异步)。
事件的顺序是在开始
callback,它本身是一个step函数,事件被触发并立即执行。这个执行的事件包含一个等待
调用,在当前时间之后添加一个延迟的步骤(我们仍然在开始
回电话)。然后执行 echo
,并且当前步骤完成。下一步开始等待60秒。由于没有回调传递给等待
下一个计划的步骤被执行。这是包含 echo('done')
的最后一步。
所以严格来说然后
不等待上一步的事件执行,但在这种情况下没有中断控制流(通常通过 setTimeout
),而CasperJS步进处理器已经捕获了内部等待
步骤。
I am just curious how CasperJS handles events with regards to the call stack.
Let's say we have some code:
casper.on('foo', function() {
this.wait(60000);
this.echo('foo');
});
casper.start('http://www.stackoverflow.com', function() {
this.echo('start');
this.emit('foo');
});
casper.then(function() {
this.echo('done');
});
casper.run();
I know that then() will wait check against 3 flags: pendingWait, loadInProgress, and navigationRequested. Printing out the call stack shows the emit call to be in the function start(), so will start() not be considered finished until the event is finished? I.e. will then() wait until the event has finished
I tested this with wait of 60 seconds, and I did get the output:
start
foo
done
Though I was not sure if exceeding a certain timeout would trigger the next then().
You have to keep in mind which functions are executed asynchronously. In your case the first two lines of the output are printed immediately when the page was loaded and done
was printed 60 seconds later.
Things to know:
- All
then*
andwait*
functions will insert a step into the queue, but are themselves asynchronous. casper.emit
will look for the registered event handler and execute it immediately (non-asynchronous).casper.echo
will print something immediately (non-asynchronous).
The order of events is that in the start
callback, which itself is a step function, an event is triggered and immediately executed. This executed event contains a wait
call which adds a delayed step after the current one (we're still in the start
callback). Then echo
is executed and the current step is finished. The next step begins by waiting 60 seconds. Since no callback was passed to wait
the next sheduled step is executed. This is the last step which contains echo('done')
.
So strictly speaking then
doesn't wait for the event execution of the previous step, but in this case there was no break out of the control flow (usually done through setTimeout
) and the CasperJS step processor has caught the inner wait
step.
这篇关于CasperJs然后()等待上一个函数中发出的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!