Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how we can use evaluateAsync in phantomjs

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

like image 979
MOB Avatar asked Mar 18 '14 09:03

MOB


1 Answers

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.
like image 191
Vitaly Slobodin Avatar answered Oct 17 '22 12:10

Vitaly Slobodin