Ced Ced - 1 year ago 52
MySQL Question

How to promisify a MySql function using bluebird?

Some time ago I decided to switch from PHP to node. In my first projects I didn't want to use any ORM since I thought that I didn't need to complicate my life so much learning another thing (at the moment I was learning node and angular) therefor I decided to use mysql package without anything else. It is important to say that I have some complex queries and I didn't want to learn from sctratch how to make them work using one of the 9000 ORM node have, This is what I've been doing so far:


Thing.list = function (done) {
db.query("SELECT * FROM thing...",function (err,data) {
if (err) {
} else {
module.exports = Thing;


Thing = require('thing.service.js');
Thing.list(function (err,data) {
if (err) {
res.status(500).send('Error D:');
} else {

how can I promisify this kind of functions using bluebird ? I've already tried but .... here I am asking for help. This is what I tried

var Thing = Promise.promisifyAll(require('./models/thing.service.js'));


Answer Source

I have never had much luck with promisifyAll and IMO I prefer to handle my internal checks manually. Here is an example of how I would approach this:

var Promises = require('bluebird');

Things.list = function(params) {
 return new Promises(function(resolve, reject) {
   db.query('SELECT * FROM thing...', function(err, data) {
     return (err ? reject(err) : resolve(data));


var thinger = require('ThingModule');

thinger.list().then(function(data) {
   //do something with data
.error(function(err) {