Skip to main content

JavaScript SDK: Does SDK_READY event fire only once?

Problem

When implementing the code below, sometimes the code never gets executed even though no errors are captured from the SDK Error log.

client.on(client.Event.SDK_READY, function() { 
var treatment = client.getTreatment("SPLIT_NAME");
console.log("Treatment = "+treatment);
});

Root Cause

The SDK_READY fires only once, so if the code block above is executed after the SDK_READY event is fired, it will never be triggered.

Solution

Another option to check for SDK ready is using the built-in Promise client.ready(), this can be used anytime, which gives it more advantage over checking the event only, see the example below:

client.ready().then(() => {
var treatment = client.getTreatment("SPLIT_NAME");
console.log("Treatment = "+treatment);
});