0
我有一個phantomjs節點的'waitfor'的實現,似乎sitepage.evaluate
相比,當它應該評估真正的時候有一個很大的滯後。您將在下面看到,我正在註銷內容值和內容日誌,並且應該評估爲真實,但在事實發生後,似乎並沒有發生好10秒鐘左右的事情。phantomjs-node page.evaulate似乎掛起
任何想法會導致這種延遲,或者如果有更好的方法來評估?
let Promise = require('bluebird');
let phantom = require('phantom');
let sitepage;
let phInstance;
phantom.create()
.then(instance => {
phInstance = instance;
return instance.createPage();
})
.then(page => {
sitepage = page;
return page.open('https://thepiratebay.org/search/game/0/99/0');
})
.then(status => {
return waitUntil(function() {
//This returns the correct content after a short period, while the evaluate ends up taking maybe 10s longer, after this content should evaluate true.
sitepage.property('content').then(content => {
console.log(content);
});
return sitepage.evaluate(function() {
return document.getElementById('searchResult');
});
}).then(function() {
return sitepage.property('content');
}).catch(Promise.TimeoutError, function(e) {
sitepage.close();
phInstance.exit();
});
})
.then(content => {
console.log('content');
console.log(content);
sitepage.close();
phInstance.exit();
})
.catch(error => {
console.log(error);
phInstance.exit();
});
var waitUntil = (asyncTest) => {
return new Promise(function(resolve, reject) {
function wait() {
console.log('--waiting--');
asyncTest().then(function(value) {
if (value) {
console.log('resolve');
resolve();
} else {
setTimeout(wait, 500);
}
}).catch(function(e) {
console.log('Error found. Rejecting.', e);
reject();
});
}
wait();
});
}