1

I been playing along with Node.JS and I found this great async library https://github.com/caolan/async. I want to replace the traditional callback model with this, as it looks a bit more pleasing and easier to understand.

This is my code for sql query

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;
    });

  } 

}); }

I am wondering how to make this async, like the example given in the library.

    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']
   });

Can someone help me with this? I don't quite understand how the error reporting works.

Based on Chris's comment, how exactly will the waterfall method help if called in multi layered?

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);
}

the "query_mssql()" call your function. How do pass on result back to top calling function or the error back?

1 Answer 1

1

For this I believe you should use async.waterfall since you want to pass results along each async task.

Here is a quick example below. You'll have to edit it to suit your needs.

If an error happens in a task, it'll move out to the optional method at the end.

async.waterfall([
    function(callback) {
        var connection = new sql.Connection(config, 
            function(err) {
                callback(err, connection)
            }
        );
    },
    function(connection, callback) {
        var request = connection.request(); 
        request.query(sql_string, callback);
    }
], function(err, recordset) {
    // ... error checks should go here :

    // output query result to console:
    console.log(recordset);
});

As you see, you can add parameters after the error in your callback and then receive them in the next task. This is a feature of async.waterfall.

Here is a test example as well...

require("should");
var async = require("async");
describe("async test", function() {
   it('should do stuff', function(done){

       async.waterfall([
           function(callback){
               a(callback);
           },
           function(aResult, callback){
               b(callback, aResult);
           }
       ], function(err, aResult, bResult) {
           console.log(aResult + " " + bResult);
           done();
       });

       function a(callback) {
           callback(null, 1);
       }
       function b(callback, aResult) {
           callback(null, aResult, 2);
       }

   })
});

As for your last edit, you must return your value asynchronously. So you cannot use a return statement to get your result. This has a trickle effect which causes those dependencies to become asynchronous as well.

Sign up to request clarification or add additional context in comments.

8 Comments

This won't going to work: you're not calling the callback without the error in the first function, same thing in the second one. You should necessarily do that, to let async know you're done and it can execute the next function.
I am assuming that the error will be null from those functions if they are successful. I think it is Ok under that assumption.
Can you explain? I did not understand the "async know you're done".
Chris, This is quite interesting, but will this method work when I use multi layerd calls? I will write a separate comment explaining this.
@Cris Ok, but even if the error is null, you should call the callback, like callback(null, connection), otherwise the waterfall function will never be notified that you have finished what you're doing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.