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.
source share