2016-10-26 2 views
0

나는 phantomjs-node와 함께 'waitfor'구현을 가지고 있으며, 사실로 평가해야 할 때와 비교할 때 큰 지연을 가지고있는 것 같습니다. 아래에서 볼 수 있듯이 콘텐츠 값과 콘텐츠 로그를 실제로 로그해야하는 항목으로 로그 아웃합니다. 그러나 사실 이후 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(); 
    }); 
} 

답변

0

내가 알기 론, sitepage.evaluate 함수는 Element를 반환합니다. 내 경험에 의하면 그것은 나쁜 생각입니다. sitepage.evaluate에서 짧은 문자열을 반환하는 것이 좋습니다. 예를 들어 return document.getElementById ('searchResult') 대신; 다음과 같이 쓸 수 있습니다 :

return document.getElementById('searchResult') ? 'true' : ''; 
관련 문제