peerigon / phridge

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

how can i cancel(settle) the promise of running page ? #42

Closed sodawy closed 8 years ago

sodawy commented 8 years ago

I want cancel or abort a running page, but the when promise cannot cancel.

Is there some way to abort the running promise with no exception like Cannot call reject() after the promise has already been resolved or rejected?

how about change the promise lib to bluebird, which is more popular, better api and performance.

thx!

jhnns commented 8 years ago

What do you mean by cancel?

If you don't want resolve to be called by your success event listener, a common way to prevent double resolving or rejecting is by overwriting resolve or reject with an empty function, like Function.prototype.

phantom.run(function (resolve, reject) {
    page.open("http://www.google.com", function () {
        resolve(text);
    });
    // oops, I don't want this anymore
    resolve = Function.prototype;
    reject(new Error("I'm outta here!"));
})

Does this solve your issue?

how about change the promise lib to bluebird, which is more popular, better api and performance.

With the next major version, phridge will just use the global Promise object. Then you can use your own library.

sodawy commented 8 years ago

"next major" sounds great~

the cancel i want:

javascript when.promise(function(resolve, reject){ if(somecase){ cancel(); //thenA and thenB block will not be entered } }).then(function(){//then A

}).then(function(){//then B

})



thanks again~
jhnns commented 8 years ago

That's basically what reject() does.

jhnns commented 8 years ago

phridge 2.0.0 shipped today with native promises