title: Altering array in .then statement (javascript)
body: <p>In a double for loop I am looping through different values where I test if specific value returns an error. Predefined function returns promise and it is all okay, if I need it once. But I need a way to store all errors (errors array) and all correct states (corrects array) and hardware I am using does not allow dynamically adding text to screen so I am using arrays. But i cannot find a way to do it in .then block. I was searching for a solution online but could not find one that would solve my problem.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>for (var i = 0; i &lt; idarray.length; i++) {
    type = idarray[i];
    for (var j = 0; j &lt; maxValues[i]; j++) {
      //console.log("Type: ",type);
      //here i send new command with type and value
      var sethbt = myLibrary.NewCommand(uuid, "Param", {
        "Data": [{
          "Type": type, //number
          "Control": 1,
          "Value": j.toString() //string
        }]
      })
      //here i read the value of type
      var hbt = myLibrary.NewCommand(uuid, "Param", {
        "Data": [{
          "Type": type,
          "Control": 0,
          "Value": ""
        }]
      })
      //hbt returns a promise where i get the value, if control==2 it is error, otherwise 
      //it is ok
      hbt.then(function(data) {
        obj = JSON.parse(data);
        if (obj[0].Control == 2) {
          var text = "ERROR: Type: \"" + obj[0].Type + "\" --&gt; Value:\"" + obj[0].Value + "\"";
          //here i want to add error to array of errors, but change is not seen
          //out of .then block
          errors.push(text);
        } else {
          corrects.push("OK: Type: \"" + obj[0].Type + "\" --&gt; Value:\"" + obj[0].Value + "\"");
        }
  
      });
    }</code></pre>
  </div>
  </div>
  </p>