MOB
MOB

Reputation: 853

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

Upvotes: 6

Views: 2720

Answers (1)

Vitaly Slobodin
Vitaly Slobodin

Reputation: 1359

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.

Upvotes: 10

Related Questions