title: Javascript closure not working
body: <p>I've read these questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example">JavaScript closure inside loops – simple practical example</a></li>
  <li><a href="https://stackoverflow.com/questions/111102/how-do-javascript-closures-work">How do JavaScript closures work?</a></li>
  <li><a href="https://stackoverflow.com/questions/2568966/how-do-i-pass-the-value-not-the-reference-of-a-js-variable-to-a-function">How do I pass the value (not the reference) of a JS variable to a function?</a></li>
  </ul>
  
  <p>and tried to apply their solutions (as well as at least 1/2 a dozen other implementations) and none of them are working.</p>
  
  <p>Here's the function that has the loop:</p>
  
  <pre><code>ExecuteQueryWhereQueryAndParamsBothArrays: function (queryArray, paramsArray, idsArray, success, fail, errorLogging) {
              var hasError = false;
              $rootScope.syncDownloadCount = 0;
              $rootScope.duplicateRecordCount = 0;
  
              $rootScope.db.transaction(function (tx) {
                  for (var i = 0; i &lt; paramsArray.length; i++) {
                      window.logger.logIt("id: " + idsArray[i]);
  
                      var query = queryArray[i];
                      var params = paramsArray[i];
                      var id = idsArray[i];
  
                      tx.executeSql(query, params, function (tx, results) {
                          incrementSyncDownloadCount(results.rowsAffected);
                      }, function(tx, error) {
                          if (error.message.indexOf("are not unique") &gt; 0 || error.message.indexOf("is not unique") &gt; 0) {
                              incrementDuplicateRecordCount(1);
                              return false;
                          }
  
  // this didn't work:    errorLogging(tx, error, id);
  // so I wrapped in in an IIFE as suggested:
                          (function(a, b, c) {
                              errorLogging(a, b, idsArray[c]);
                          })(tx, error, i);
  
                          return true;
                      });
                  }
              }, function () {
                  fail();
              }, function () {
                  success();
              });
  </code></pre>
  
  <p>And here's the errorLogging function that is writing my message (Note, I'm not able to "write" the message in the same javascript file because I'd need to [angular] inject another reference into this file and it would cause a circular reference and the code won't run)</p>
  
  <pre><code>var onError = function (tx, e, syncQueueId) {
      mlog.LogSync("DBService/SQLite Error: " + e.message, "ERROR", syncQueueId);
  };
  </code></pre>
  
  <p>What other method can I implement to stop it from returning the very last "id" of my sync records (when it's only the first record that has the error)?</p>