title: Nodejs fast-csv and promises
body: <p>I feel like things are running out of order here.  I want to establish a connection to a mySQL database.  Then I want to read in a file.  Row by row I want to grab the name and run a query.  I would have assumed that my sqlSelectQuery function, which returns a promise, would have waited for the promise to resolve before moving onto the next row.  What am I missing here?</p>
  <pre><code>const mysql = require('mysql');
  const fs = require('fs');
  const path = require('path');
  const csv = require('fast-csv');
  const config = require('./config')
  
  const connection = mysql.createConnection({
      user: config.user,
      password: config.password,
      database: config.database,
      host: config.host
  });
  
  connection.connect((err) =&gt; {
      if(err){
        console.log('Error connecting to Db');
        return;
      }
      console.log('Connection established');
    });
  
  fs.createReadStream(path.resolve(__dirname,'data.csv'))
      .pipe(csv.parse({ headers: true }))
      .on('error', error =&gt; console.error(&quot;error&quot;, error))
      .on('data', row =&gt; { // need to get this to block
          sqlSelectQuery(row).then(result =&gt; console.log(&quot;result: &quot;, result))
      })
      .on('end', rowCount =&gt; console.log(`Parsed ${rowCount} rows`));
  
  
  
  
  const sqlSelectQuery = (row) =&gt; {
      return new Promise((resolve, reject) =&gt; {
          console.log(&quot;inside promise&quot;);
          const selectQuery = 'SELECT * FROM loans where business_name = ?;';
          connection.query(selectQuery, [row.BorrowerName], (err,rows) =&gt; {
              let result = {};
              if(err) reject(err);
              if (rows.length === 1){
                  let res = rows[0];
                  result = {
                      business_name: res.business_name,
                      loan_range: res.loan_range,
                      loan_amount: row.InitialApprovalAmount,
                      count: 1
                  };
                  resolve(result);
              } else {
                  result = {
                      business_name: row.BorrowerName,
                      loan_range: &quot;&quot;,
                      loan_amount: &quot;&quot;,
                      unique: rows.length
                  };
                  resolve(result);
              }
          });
      })
  }
  
  my console looks like this
  inside promise
  inside promise  //20 times (I have 20 rows)
  Parsed 20 rows
  Connection established
  result:  {....}
  result: {...}....
  </code></pre>