I am receiving various key-value pairs from a eventemitter which need to be compiled into an object. Tricky part is that some of the needed key-values are time delayed. I am using a switch statment to write the values to temporary variables.
switch (
data.keys
case "key1_1":
tempvalue1_1 = data.value;
break;
case "key1-2":
tempvalue1_2 = data.value;
break;
case "key1-3":
tempvalue1_3 = data.value;
break;
case "key1-4":
tempvalue1_4 = data.value;
break;
as soon as data is being received I check using an if statment if the data is plausible (not null) and push it into my needed object
if (
tempvalue1_1 &&
tempvalue1_2 &&
tempvalue1_3 &&
tempvalue1_4
) {
let object = {
key1-1: "key1-1",
key1-1_value: tempvalue1_1,
key1-2: "key1-2",
key1-2_value: tempvalue1_2,
key1-3: "key1-3",
key1-3_value: tempvalue1_3,
key1-4: "key1-4",
key1-4_value: tempvalue1_4,
};
}
As soon as the object is compiled I send it off to elasticsearch afterwards I reset the key-values to null.
As stated before my problem is that some key-values are a bit time delayed. Which means key-value pair 1-1 and 1-2 always changes but key-value pair 1-3 and 1-4 does not always change as soon as key-value1-1 and 1-2 change and will be emitted up to 500ms after 1-1 and 1-2. I tried to use
setTimeout(function(){ },500)
within my above mentioned if statment but this does only makes the if statment true multiple times and builds the object with uncomplete data.
Any suggestions?