title: Nodejs SQL connections and async module
body: <p>I been playing along with Node.JS and I found this great async library <a href="https://github.com/caolan/async" rel="nofollow">https://github.com/caolan/async</a>. I want to replace the traditional callback model with this, as it looks a bit more pleasing and easier to understand.</p>
  
  <p>This is my code for sql query</p>
  
  <pre><code>function query_mssql(config, sql_string){
    var connection = new sql.Connection(config, function(err) {
    // ... error checks 
  
    if (err) {
      console.log('connection to mssql has failed');
      //throw err;
    }else{
  
      // Query 
      var request = new sql.Request(connection); // or: var request = connection.request(); 
      request.query(sql_string, function(err, recordset) {
          // ... error checks should go here :
  
          // output query result to console:
          console.log(recordset);
          return recordset;
      });
  
    } 
  </code></pre>
  
  <p>});
  }</p>
  
  <p>I am wondering how to make this async, like the example given in the library.</p>
  
  <pre><code>    async.series([
      function(callback){
          // do some stuff ...
          callback(null, 'one');
      },
      function(callback){
          // do some more stuff ...
          callback(null, 'two');
      }
     ],
     // optional callback
     function(err, results){
      // results is now equal to ['one', 'two']
     });
  </code></pre>
  
  <p>Can someone help me with this? I don't quite understand how the error reporting works.</p>
  
  <p>Based on Chris's comment, how exactly will the waterfall method help if called in multi layered?</p>
  
  <pre><code>function computeCurrentDefinitionResult(database, node_name) {
  async.waterfall([
      function(callback) {
        var leaf_sql_query = "SELECT * FROM "+  JSON.stringify(database)  +".dbo.LeafNode WHERE NodeName=" + "'" + node_name + "'";
        query_mssql_internal(leaf_sql_query, callback);
        console.log('Might BE HAPPY');
      },
  ], function(err, recordset) {
      // ... error checks should go here :
      if (err) {
          console.log('mssql query has failed');
      }
  
      // output query result to console:
      console.log(recordset);
      return recordset;
    });
  
  function query_mssql_internal(sql_string){
    return query_mssql(config, sql_string);
  }
  </code></pre>
  
  <p>the "query_mssql()" call your function. How do pass on result back to top calling function or the error back?</p>