how we can use evaluateAsync in phantomjs

岁酱吖の 提交于 2019-12-10 13:26:31

问题


what's the usage of evaluateAsync and when we have to use this function and what's the benefit of using this function . in the below we see a poor documentation for this :

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.evaluateJavaScript example.

any body can show a example of usage of evaluateAsync in phantomjs


回答1:


This function allows you to execute any JavaScript code like the evaluate API function. But it will evaluate your code asynchronous. It means:

  • Current execution context will not be blocked.
  • It will not return any result.

Let's say you want execute some long-running JavaScript code, but you don't interested in its result. If you will use evaluate, your current execution context will be blocked.

The documentation for evaluateAsync is a bit wrong. The correct signature for evaluateAsync is: evaluateAsync(function, ms, args), where:

  • function - the function to evaluate
  • ms - time to wait before execution
  • args - function arguments

Example:

evaluateAsync(function() {
   console.log('Hi! I\'m evaluateAsync call!');
}, 1000);

Using in the real world:

  • You want to capture some asynchronous events.
  • Unit testing! AFAIK, PhantomJS runners use evaluateAsync to run unit tests.


来源:https://stackoverflow.com/questions/22474525/how-we-can-use-evaluateasync-in-phantomjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!