redis-js icon indicating copy to clipboard operation
redis-js copied to clipboard

Add .ready promise to Subscriber

Open controversial opened this issue 8 months ago • 0 comments

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 .ready is 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

controversial avatar Apr 15 '25 03:04 controversial