title: node.js async/await function called multiple times (before it ends)
body: <p>My main file is a &quot;file watcher&quot; that's watching a folder using &quot;node-watch&quot; module. For each file placed in the folder, it has to do couple of MSSQL calls and then process the file accordingly.
  The file watcher script looks like that:</p>
  <pre><code>var watch = require('node-watch');
  const timestamp = require('./SHR_modules/timestamp');
  const sql = require('./SHR_modules/sql');
  const configuration = require('./SHR_modules/config');
  const logger = require('./SHR_modules/logger');
  
  watch('./Incoming', { recursive: true }, async function (evt, name) {
  
    if (evt == 'update') {
      logger.addLog(configuration.logFile.watcher, 'File found: ' + name);
      var startTime = await timestamp.dateTimeDelimited();                //capture time at proicessing start
      filePath = await name.replace(name.split('\\')[name.split('\\').length-1], '');
      fileName = await name.replace(filePath, '');
      var myLoad = await sql.readLoadSpec(fileName, filePath).catch(err =&gt; {
        logger.addLog(configuration.logFile.error, 'Error while reading Load Specs for file: ' + name);
        logger.addLog(configuration.logFile.error, err);
      });
      if (typeof myLoad !== 'undefined' &amp;&amp; myLoad){
        console.log(evt);
        logger.addLog(configuration.logFile.watcher, 'Finished processing: ' + name);
      };
      var finishTime = await timestamp.dateTimeDelimited();                  //capture time at processing finish
    }
    else {
      logger.addLog(configuration.logFile.watcher, 'File removed: ' + name);
    }
    console.log(startTime);
    console.log(finishTime);
  });
  </code></pre>
  <p>and the SQL function:</p>
  <pre><code>var readLoadSpec =  (fileName, filePath) =&gt; {
      return new Promise(async (resolve, reject) =&gt; {
          specIDs = await findSpecs(fileName, filePath)
          .catch(err =&gt; {
              reject(err);
              console.log(fileName + ' not found')
          });
          if (typeof specIDs !== 'undefined' &amp;&amp; specIDs) {
              console.log('FOUND!!!!');
              var addLoadSpec = [];
              for (let i = 0; i &lt; specIDs.length; i++) {
                  console.log(specIDs);
                  var tempQuery1 = `select * from LoadSpec where LoadID = ${specIDs[i]}`;
                  var tempQuery2 = `select * from Mappings where LoadID = ${specIDs[i]} ORDER BY OutputColumn`;
                  console.log(specIDs);     &lt;========= code work sup to this line
                  const results = await queryDB(tempQuery1);
                  const resultsMapping = await queryDB(tempQuery2);
                  console.log(fileName);
                  console.log(results.recordset[0].OutputFileName);
                  console.log(specIDs);
                  const inputFrmt = {
                      name: results.recordset[0].InputFormatName,
                      format: results.recordset[0].InputFormat,
                      delimiter: results.recordset[0].InputDelimiter,
                      headerRows: results.recordset[0].InputHeaderRows
                  };
  .
  .
  .
                  console.log(results.recordset[0].OutputColumnCount);
                  addLoadSpec.push(loadSpec);
              };
              resolve(addLoadSpec);
          };
      });
  };
  </code></pre>
  <p>So the code runs to the row I've marked (before the await for query results) and the loops back to beginning.
  So all is fine if I drop a single file into the folder, but when multiple files arrive at more or less the same time, the syncIDs var takes value of another file, before the code resumes after the query await part is completed. syncIDs is just an array so let's say for file 'a.csv' it's [1,2], for file 'b.csv' it's [3,4] and for file 'c.csv' it's undefined. So before it get's the answer from the DB, this loop:</p>
  <pre><code>specIDs = await findSpecs(fileName, filePath)
          .catch(err =&gt; {
              reject(err);
              console.log(fileName + ' not found')
          });
          if (typeof specIDs !== 'undefined' &amp;&amp; specIDs) {
              console.log('FOUND!!!!');
              var addLoadSpec = [];
              for (let i = 0; i &lt; specIDs.length; i++) {
  </code></pre>
  <p>Get's error for index of undefined</p>
  <p>How do I make it process files one by one?</p>