JamesBarwell / rpi-gpio.js

Control Raspberry Pi GPIO pins with node.js
MIT License
657 stars 116 forks source link

Expose EventEmitter methods on gpio.promise #103

Closed pimterry closed 4 years ago

pimterry commented 4 years ago

The docs say:

This API exposes a Promises interface to the module. All of the same functions are available, but do not take callbacks and instead return a Promise.

That's not quite true, because this doesn't work:

const gpio = require('rpi-gpio').promise;
gpio.on('change', console.log); // TypeError: gpio.on is not a function

This PR fixes that in the simplest way, by just exposing all the interesting event emitter methods from GPIO on the promise object, bound back to GPIO.

These methods don't actually use promises of course, but being able to use .promise as a drop-in replacement is super useful imo, and from the docs above it seems like that's what you're aiming for.

JamesBarwell commented 4 years ago

Good spot, thanks for the PR. It looks like we can't proxy off because that didn't exist until node v10, which is why the tests are failing. Could you update the PR to remove that one? Happy to get this merged when it's passing.

I think it's correct that the event bindings are not promised based. I think callbacks are always the appropriate interface for these.

JamesBarwell commented 4 years ago

Thanks! Published as 2.1.7.

pimterry commented 4 years ago

Great stuff, thanks!