opened
gizmodude4
username_0
Hey Pete!

I found a use case for removing my `onError` case. I'm using Phantasma under the sheets of some software and I want to catch if an `onError` event happens and return that out to my Promise chain so I can terminate it. I did this with the following code:

```js
return new Promise(function(resolve, reject){
    // Attach our function to the onError handler
    var onErrorCallback = function(msg, trace){
        var fullTrace = buildStackTrace(msg, trace);
        if (fullTrace.indexOf('phantomjs') > -1){
          reject(fullTrace);
        }
      };
    self.phantasma.on('onError',onErrorCallback);
    self.phantasma.evaluate.apply(self.phantasma, evalArgs)
      .then(function(result){
        // The function worked and we have a result. Remove
        // the catching function.
        self.phantasma.removeListener('onError', onErrorCallback);
        resolve(result);
      });
  })
```
I've tested in my framework and I'm no longer getting the message I was doing before, which was:

`(node) warning: possible EventEmitter memory leak detected. 11 onError listeners added. Use emitter.setMaxListeners() to increase limit.`

I'm not quite sure how to test this in the test bench you've set up. Maybe you can inform? Let me know if I can help in validating this.
Adding removeListener function.
issue
created
petecoop
username_1
Hey thanks for the PR!

This looks good, it's obviously needed. You'll need to add a test to `test/index.js`, if you look where it already tests that the `onError` event works you can put it somewhere there. To run the tests you'll need mocha installed `npm install -g mocha`, then run `npm run test`.

Here's an example of a test I just thought of for this:
```
it('should remove event listeners', function (done) {
  var count = 0;
  var errorCallback = function (msg) {
    count++;
    msg.should.not.be.empty;
  };
  ph.open('http://localhost:3000/jserr.html')
    .on('onError', errorCallback)
    .then(function () {
      ph.removeListener('onError', errorCallback);
    })
    .open('http://localhost:3000/jserr.html')
    .delay(100)
    .then(function () {
      count.should.equal(1);
    });
});
```

So we're basically just checking that the `errorCallback` was only called once - after loading the error page twice
comment
created
gizmodude4
username_0
@username_1 Thanks for the help! I've added a test that looks almost identical to your suggestion. Really clever solution.
comment
created
petecoop
username_1
Perfect, thanks. I've published this to npm as `1.4.0`
comment