Issue
When implementing NodeJS SDK with Redis storage, the getTreatment method is a wrapper for redis fetch call which returns a promise, which works fine with then() and catch() blocks.
However, testing the SDK code in localhost mode below:
client
.getTreatment('user_id', 'my-feature-comming-from-redis')
.then(treatment => {
// do something with the treatment
})
.catch(() => false)
It return the error below:
splitClient.getTreatment(...).then is not a function
Root cause
As mentioned when using redis storage getTreatment() function is a wrapper of a promise returned by redis library. So when no redis call is used in the localhost mode, the same code will error out.
Answer
The SDK getTreatment method can be wrapped in async function, then use that function with then() and catch() blocks, as shown in sample below:
const path = require('path');
const SplitFactory = require('@splitsoftware/splitio').SplitFactory;
async function createSplitClient() {
const SplitObj = SplitFactory({
core: {
authorizationKey: 'localhost'
},
startup: {
readyTimeout :10
},
features: path.join(__dirname, 'first.yaml'),
debug: true
});
const client = SplitObj.client();
await client.ready()
console.log("SDK is ready");
return client
}
async function getSplitTreatment(userKey, splitName) {
let splitClient = await createSplitClient()
return await splitClient.getTreatment(userKey, splitName);
}
getSplitTreatment("user", "first_split").then((treatment)=> {
console.log("treatment: "+treatment)
})
.catch(() => {
console.log("SDK exception")
});
Comments
0 comments
Please sign in to leave a comment.