I'm using trying to use nodejs and phantomjs on the server-side for SEO of our site. While ajax works fine, I'm not able to execute custom promises that I've used in my code. How do I make phantomJS wait till the promises are resolved. Below is what I have coded.
$('body').addClass('before-dom-ready');
$(function() {
$('body').addClass('after-dom-ready');
var dfrd = $.Deferred(),
promise = dfrd.promise();
setTimeout(function() {
dfrd.resolve();
}, 5000);
promise.done(function() {
$('body').addClass('promise-executed');
});
});
phantomJS adds 'before-dom-ready' and 'after-dom-ready' class, but I'm unable to get 'promise-executed' class on body.
PhantomJs does not automatically wait the end of all pending scripts. WebPage#onLoadFinished is called on the onload event.
As for most of the scripts, the idea here is to wait until "something" is done or true. I highly suggest you to test waitfor.js. It is really important to understand this example in PhantomJs.
I suppose your example is an example, but let me propose an answer.
Html Page
PhantomJs Script
Basically,
waitFor
will check every 500 ms if body has a class named'promise-executed'
.