Reputation: 336
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?
Upvotes: 1
Views: 2002
Reputation: 2806
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.
Upvotes: 1