launchdarkly / node-server-sdk

LaunchDarkly Server-side SDK for Node
Other
79 stars 65 forks source link

use .waitForInitialzation().then(...) instead of .once('ready',...) #110

Closed ghost closed 5 years ago

ghost commented 6 years ago

In the docs, we are directed to create a client via const ld_client = LaunchDarkly.init('SOME_KEY');

Then in our server's request handler, we are directed to this code:

ld_client.once('ready', function() {
   ld_client.variation("your.flag.key", {"key" : "user@test.com"}, false, function(err, show_feature) {
     if (show_feature) {
         # application code to show the feature
     } else {
         # the code to run if the feature is off
     }
   });
 });

However, our Node app would work the first time a request came through, but time out on the second request. Why? Because our app was waiting for the 'ready' event, and it never came. So, we went with the following, based on what we found in the source code:

 ld_client.waitForInitialization().then(() => {
   ld_client.variation("your.flag.key", {"key" : "user@test.com"}, false, function(err, show_feature) {
     if (show_feature) {
         # application code to show the feature
     } else {
         # the code to run if the feature is off
     }
   });
 }).then(console.log);

and now our node server can handle requests again, and we can update our LaunchDarkly settings and see the variations.

My only issue to please update the documentation. Thank you!

eli-darkly commented 6 years ago

The intention was not to wait for the ready event during every request, but just at startup time. By definition, using once to listen for an event will only ever call the handler once, so it would never make sense to use once in a request handler. We'll try to clarify the documentation.

ghost commented 6 years ago

The once ready in the documentation didn’t make sense in the request handler for me either.

However, it wasn’t documented on what to do in the request handler.

bwoskow-ld commented 5 years ago

I update the documentation to clarify that the ready event is only emitted once, and that production code shouldn't depend on using that.