peerigon / phridge

A bridge between node and PhantomJS
The Unlicense
519 stars 50 forks source link

Unsure about how to call resolve() #13

Closed jtompkins closed 10 years ago

jtompkins commented 10 years ago

I have a script to capture a webpage using phridge:

var phridge = require('phridge');

var capture = function(url, outputPath) {
  phridge.spawn()
  .then(function(phantom) {
    return phantom.openPage(url);
  })
  .then(function(page) {
    page.run(url, outputPath, function(pageUrl, outputUrl) {
      this.open(pageUrl, function(status) {
        this.render(outputUrl);
        resolve();
      });
    });
  });
};

capture('http://www.joshtompkins.com', 'test_img.png');

Unfortunately, the promise retuned by page.run seems to finish before the render method finishes executing, and I get the following error:

Error: Cannot resolve value: The response has already been closed. Have you called resolve/reject twice?

As you can see, I haven't called resolve twice, at least as far as I can tell. Can you give me a clue about what I'm doing wrong?

jtompkins commented 10 years ago

Edit:

I see from one of the examples that I need to add resolve and reject as arguments to the page.run call. Doing this fixes the error, but the process does not terminate and runs forever. Still not sure what's happening.

jtompkins commented 10 years ago

Edit 2: Disposing of the phantomjs process properly terminates the program. Sorry for the confusion!

jhnns commented 10 years ago

No problem. I'm glad that the docs and examples could help you. :grinning: