0

I have learned programming asynchronous PhantomJS code, but now I am planning to write synchronous JavaScript. I have known promise can do this.

How can I write synchronous JavaScript code for PhantomJS?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222

1 Answers1

1

I am planning to write synchronous JavaScript. I have known promise can do this.

That's not exactly true. Promises still require asynchronous programming, but they are good to escape the callback hell.

How can I write synchronous JavaScript code for PhantomJS?

The PhantomJS API uses callbacks to work. It doesn't emit promises that you can use. Of course you could write a wrapper around PhantomJS to support a promise-like API, but that would be overkill. Have a look at CasperJS which is built on top of PhantomJS (and Slimer.js) and does provide an API similar to the promise syntax, but there are differences. I have an answer that shows some intricacies of using CasperJS.

Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Is there any way to use thirdparty components like step or async ? Just like nodejs. – cloudsky Sep 30 '15 at 00:43
  • Yes, you can use async in PhantomJS. PhantomJS' API module system is completely different from node.js, but it has one. – Artjom B. Sep 30 '15 at 07:12
  • Can you show some examples? I have not got async module in src/modules of phantomjs source codes. – cloudsky Sep 30 '15 at 09:19
  • Just install async.js through npm and require it. [Here](http://stackoverflow.com/questions/26681464/looping-over-urls-to-do-the-same-thing) is an application example. – Artjom B. Sep 30 '15 at 10:18
  • I use phantomjs in windows. Can i compile async.js in phantomjs or require it as thirdparty components? – cloudsky Sep 30 '15 at 12:31
  • You don't compile JavaScript code, but other than that yes. – Artjom B. Sep 30 '15 at 15:56