title: How to refactor this node.js code, to avoid duplication
body: <p>I am trying to trying to setup a base skeleton for my demo project and writing my first node.js program. The below code is working for my first test, but it has duplicate - Getting connection, Executing a query (but a different one), parsing the sql output. I want to minimize this, because, I have to write more if-else blocks like that and it will become humongous and dirty code.</p>
  
  <p>Database connection details are handled in <strong>DatabaseManager.js</strong></p>
  
  <p><strong>app.js</strong></p>
  
  <p>Line #8 and #40:</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>var connect = require('./DatabaseManager');
  bot.dialog('profileDialog', (session) =&gt; {
      session.send('You reached the profile intent. You said \'%s\'.', session.message.text);
      console.log('Creating a connection');
  
      var userMessage = session.message.text;
  
      // Here is the FIrst block
      if (userMessage.indexOf('Email') &gt;= 0) {
        session.send('Your are looking for your email');
  
        connect(function(connection) {
          console.log('Reading rows from the Table...');
  
          request = new Request("select Email from StudentProfile where ID=1", function(err, rowCount) {
  
            if (err) {
              console.log('ERROR in QUERY');
            } else {
              console.log(rowCount + ' rows');
            }
            connection.close();
          });
  
          request.on('row', function(columns) { // Iterate through the rows using a callback
            columns.forEach(function(column) {
              if (column.value === null) {
                console.log('NULL');
              } else {
                session.send(column.value);
              }
            });
          });
          connection.execSql(request);
        });
        session.endDialog();
        return;
      } //end of email id if
  
      //Here is the second block with repeated functionality, but a different query.
      connect(function(connection) {
        console.log('Reading rows from the Table...');
  
        request = new Request("select FNAME from StudentProfile where ID=1", function(err, rowCount) {
  
          if (err) {
            console.log('ERROR in QUERY');
          } else {
            console.log(rowCount + ' rows');
          }
          connection.close();
        });
  
        request.on('row', function(columns) { // Iterate through the rows using a callback
          columns.forEach(function(column) {
            if (column.value === null) {
              console.log('NULL');
            } else {
              session.send(column.value);
            }
          });
        });
        connection.execSql(request);
      });
  
  
    } //end of dialog
  
  ).triggerAction({
    matches: 'profile'
  }) //end of trigger</code></pre>
  </div>
  </div>
  </p>
  
  <p>I want to refactor this code to make it simple and reusable by other functionalities by passing required arguments.</p>
  
  <p>I tired the below way, but <code>return (column.value);</code> statement is not working:</p>
  
  <pre><code>function queryDatabase(colname) {
      connect(function (connection) {
          request = new Request('select Email from StudentProfile where SUID=1', function (err, rowCount) {
  
              if (err) {
                  console.log('ERROR in QUERY');
                  console.log(err);
              } else {
                  console.log(rowCount + ' rows');
              }
              connection.close();
          });
  
          request.on('row', function (columns) {  // Iterate through the rows using a callback
              columns.forEach(function (column) {
                  if (column.value === null) {
                      console.log('NULL');
                  } else {
                      console.log(column.value);
                      return (column.value);
                  }
              });
          });
          connection.execSql(request);
      });
  }
  bot.dialog('profileDialog',(session) =&gt; {
      session.send('You reached the profile intent. You said \'%s\'.', session.message.text);
      console.log('Creating a connection');
  
      var userMessage = session.message.text;
  
      if( userMessage.indexOf('Email') &gt;= 0){
          session.send('Your are looking for your email');
          var messg = queryDatabase('Email');
          console.log(messg);
         session.endDialog();
         return;
       } //end of email id if
  
       else {
         session.send('Looking for something else');
         session.endDialog();
         return;
       }
  
  
  } //end of dialog
  
  ).triggerAction({
  matches: 'profile'
  }) //end of trigger
  </code></pre>