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?