phantomjs waitfor example and javascript async processes..

1,463 views
Skip to first unread message

bruce

unread,
May 15, 2012, 2:18:48 PM5/15/12
to phan...@googlegroups.com, casp...@googlegroups.com, Nicolas Perriault
Hi.

Created a quick/basic test for phantom, to do a waitfor. i modified
the waitfor phantom script to return a false/true if it timesout, or
succeeds.

The test basically uses a "return (true) as the testFX, and I then
have a logic block based on the returned results of the waitfor.

My issue/question is that the javascript test and the waitfor function
appear to be async, in that the code in the calling function continues
to process, without waiting for the waitfor to return.

Is there away to force the app to wait for the waitfor function to complete???

My short test follows::

function waitFor2(testFx, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3001, //<
Default Max Timeout is 3s
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) &&
!condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ?
eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but
condition is 'false')
//console.log("'waitFor()' timeout");
console.log("falsem");
//phantom.exit(1);
clearInterval(interval); //< Stop this interval
return(false);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
//console.log("'waitFor()' finished in " + (new
Date().getTime() - start) + "ms.");
//typeof(onReady) === "string" ? eval(onReady) :
onReady(); //< Do what it's supposed to do once the condition is
fulfilled
clearInterval(interval); //< Stop this interval
console.log("ftt");
return(true);
}
}
}, 100); //< repeat check every 100ms
};


// Route "console.log()" calls from within the Page context to the
main Phantom context (i.e. current "this")
page.onConsoleMessage = function(msg) {
console.log(msg);
};

//page.open(encodeURI(searchURL));
add=searchURL;

//page.onLoadFinished = function(status) {

page.open(searchURL, function(status){
if (status !== "success") {
console.log("Unable to access network");
phantom.exit();
}
else
{
res=waitFor2(function(){
return page.evaluate(function() {
return false;
});
})

console.log("111");


if(res==false) //// <<<<< gets processed before the above wait
call completes!!!
{
return (false);
}
else if(res==true)
{
return (true);
}

console.log("aaa 111");

}
});

Mark Steward

unread,
May 16, 2012, 4:35:35 AM5/16/12
to phan...@googlegroups.com
That's not how waitfor works. You should read up on callbacks in Javascript:

http://www.slideshare.net/domenicdenicola/callbacks-promises-and-coroutines-oh-my-the-evolution-of-asynchronicity-in-javascript

(first hit when googling). You seem to have a lot of questions related to Javascript as a language, rather than Phantomjs. I don't think this group is the best place to ask them.

bruce

unread,
May 16, 2012, 8:57:48 AM5/16/12
to phan...@googlegroups.com
Hey Mark,

A little confusion. You say that's not how the Waitfor works... What
is is that I've stated that's not correct? Are you saying that the
Waitfor doesn't return a True/False for the input test function? Are
you saying that it doesn't return a result to the calling parent, that
the calling parent can then wait on, in order to continue the logic
process?

i think I understand that the Waitfor function for phantomJS works,
using the callback for the testFX function as well as the callback for
the "True" situation, which is the function that gets invoked if the
wait test is true.

But this approach is anathema to other procedural processes I've used.
i also understand the apparent async nature of javascript. Or are you
saying that javascript/phantomJS really should allow a function to be
called, and then wait until the function is complete??

Or are you saying I don't understand javascript not having a
sleep/halt/wait function like other languages?

So,I'm confused about what it is you're saying I'm missing/not
understanding. Could you please clarify? Now, don't get me wrong,
while I used to be the guy doing the development, I haven't done
development of any significance in over 15 years.


Thanks
Reply all
Reply to author
Forward
0 new messages