Does CasperJs () start waiting for outgoing events in a previous function?

I'm just wondering how CasperJS handles events regarding the call stack.

Say we have a 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 for a check against 3 flags : pendingWait, loadInProgress and navigationRequested. A listing of the call stack indicates that the emit call is in the start () function, so start () is not considered complete before the event ends? That is, then () wait until the event ends

I tested this with a wait of 60 seconds and I got the result:

 start foo done 

Although I was not sure that exceeding a certain timeout would cause the next, then ().

+6
source share
1 answer

You must remember what functions are performed asynchronously. In your case, the first two lines of output are printed immediately after loading the page and done were printed after 60 seconds.

What you need to know:

  • All then* and wait* functions will insert a step into the queue, but they themselves are asynchronous.
  • casper.emit will search for a registered event handler and execute it immediately (non-asynchronous).
  • casper.echo will print something immediately (non-asynchronously).

The order of events is that in the start callback, which itself is a step function, the event is triggered and immediately executed. This completed event contains a wait call that adds a delayed step after the current one (we are still in the start callback). Then echo is executed, and the current step is complete. The next step begins with a wait of 60 seconds. Since the callback was not sent to wait , the next step is performed. This is the last step that contains echo('done') .

Thus, strictly speaking, then does not wait for the event of the previous step to complete, but in this case there was no exit from the control flow (this is usually done through setTimeout ), and the CasperJS step processor caught the internal wait step.

+3
source

Source: https://habr.com/ru/post/980487/


All Articles