I'm wondering if this approach is correct or does it need further refinements, maybe promisify custom mySQL getConnection
method as well???
request: function(queryRequest) {
return new Promise(function(resolve, reject){
Database.getConnection(function(error, connection){
if(error) {
reject({error: error, queryRequest: queryRequest});
} else {
connection.query(queryRequest.sql, queryRequest.values, function(error, rows, fields){
if(error) {
reject({error: error, queryRequest: queryRequest});
} else {
resolve({rows: rows, fields: fields, queryRequest: queryRequest});
}
});
}
});
});
},
The getConnection
method defined in Database
module.
getConnection: function(callback) {
this.pool.getConnection(function(error, connection){
callback(error, connection);
});
},
Only maybe. While it could be considered a bit cleaner, and makes your callback pyramid a bit flatter, it doesn't improve the code much:
For database connections, you might want to have a look at the disposer pattern. If you don't need it, you still should remember to always release your connections, using something like
Also, rejecting promises with objects that are no
Error
s is a bad practise, you might better door the equivalent in your original pattern.