I have a use case where I need to run multiple tasks in series. Each task is required to perform some database operations. In my code step_1
, step_2
and step_3
are these task which get executed in series (Async Waterfall)
Following is my code
const config = require('config.json');
var mysql = require('mysql');
var async = require("async");
const pool = mysql.createPool({
host: config.database.dbhost,
user: config.database.dbuser,
password: config.database.dbpassword,
database: config.database.dbname
});
exports.handler = (event, context, callback) => {
pool.getConnection((err, connection) => {
if (err) throw new Error("Unable to get connection " + JSON.stringify(err));
async.waterfall([
step_1,
step_2,
step_3
], function (err, result) {
if(err){
throw new Error("something went south : "+ JSON.stringify(err));
}else{
connection.release();
callback(null, "SUCCESS: " + JSON.stringify(result));
}
});
function step_1(cb){
connection.query("step 1 query", function (error, response) {
if (error){
cb(null,error,null);
}
let id=result.insertId;
cb(null,null,id);
});
}
function step_2(error,id,cb){
if(error){
cb(null,error);
}else{
connection.query("step 2 query",,function (err,result){
if (err){
connection.query("error logging query",
function(er,result){
if (er) cb(null,er,null);
});
cb(null,err,null);
}
cb(null,null, id, result);
});
}
}
function step_3(error, id,result,cb){
if(error){
cb(error,null);
}else{
connection.query("step 3 query",function(err,result){
if (err) cb(err,null);
cb(null,result);
});
}
}
});
};
Although I have used a library to avoid nesting callbacks within callbacks, it still feels like bit callback-ish. Is there a better way to achieve my use case? Anything I can improve in my code, any suggestions please.
I have only included relevant parts of code, kindly let me know if any additional information is required.