I have written JavaScript code for fetching data from a MySQL database. I have used connection pooling and Promise API.
var mySql = require("mysql");
var Promise = require("promise");
var pool = mySql.createPool({
host : "localhost",
user : "root",
password : "rahul",
database : "testDb", //schema
connectionLimit : 13, // at a time 13 connection be created in pool
});
function getDepartments(){
return new Promise(fn);
function fn(resolve,reject){
pool.getConnection(function(err,con){
if(err){
return reject(err);
}else{
con.query("select * from departmentTbl",function(err,rows){
if(err){
return reject(err);
}else{
con.release(); // releasing connection to pool
return resolve(rows);
}
});
}
}); // getConnection
}// fn
} // getDepartments
getDepartments().then(function(rows){
console.log(rows);
}).catch(function(err){
console.log(err);
}).done(function(){
pool.end(); // closing all connections in pool
});
Can I further improve the code? I have only used one Promise. Can I use multiple promises to avoid callback hell?