redis-js
redis-js copied to clipboard
Add .ready promise to Subscriber
Background: Most commands return a promise that resolves when the command is complete/executed and its result can be relied upon, however .subscribe and .psubscribe return Subscriber instances before the SUBSCRIBE commands have completed their round trip to Redis, so it’s hard to know when the subscription(s) are ready, connected, and listening. This problem can be seen in the tests, which used a 500ms delay to try to ensure that the subscription was ready before sending PUBLISH commands
As an alternative, this PR adds a .ready property to Subscriber which clients can await to know when the subscription(s) are ready, i.e. when it’s safe to start publishing commands.
- I’m not sure whether
.readyis the best name for this promise - this is my first contribution so I’m not sure whether there’s anything I’m missing design-wise
- I updated the tests with the intended usage