Bluebird: Proper way to promisify npm mysql module

Created on 22 Mar 2016  路  3Comments  路  Source: petkaantonov/bluebird

I was going through the documentation this morning, and stumbled across the following example for promisifying the npm mysql module:

var Pool = require("mysql/lib/Pool");
var Connection = require("mysql/lib/Connection");
Promise.promisifyAll([Pool, Connection]);

This seems to work, however, I'm curious how it is different than this:

var mysql = require('mysql');
Promise.promisifyAll(mysql);
Promise.promisifyAll(require("mysql/lib/Connection"));
Promise.promisifyAll(require("mysql/lib/Pool"));

which doesn't work. I ask because I've always been used to promisifying the mysql module by directly promisifying the prototypes, like so:

var mysql = require('mysql');
Promise.promisifyAll(mysql);
Promise.promisifyAll(require("mysql/lib/Connection").prototype);
Promise.promisifyAll(require("mysql/lib/Pool").prototype);

and so I'm just curious, what is the "proper" way to promisify this module?

I'm running v3.3.4 of the bluebird module and v4.1.1 of Node.js. Thanks!

Most helpful comment

I ran into the same thing, assuming that require('bluebird').promisifyAll(MyClass) would DTRT and promisify each prototype method.

Would you accept a PR that changes promisifyAll so it detects that a function is passed and dives into the prototype in that case? Or would it introduce ambiguity?

All 3 comments

There is also an instance in the documentation in http://bluebirdjs.com/docs/api/promisification.html where the first of those three promisifications is omitted, and now I'm not sure if it was intentional:

var mysql = require('mysql');
Promise.promisifyAll(require("mysql/lib/Connection").prototype);
Promise.promisifyAll(require("mysql/lib/Pool").prototype);

The array is basically a module of classes, that's why it works. If you have separate classes like Pool, Connection you need to promisify their prototypes

I ran into the same thing, assuming that require('bluebird').promisifyAll(MyClass) would DTRT and promisify each prototype method.

Would you accept a PR that changes promisifyAll so it detects that a function is passed and dives into the prototype in that case? Or would it introduce ambiguity?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rohitbegani picture rohitbegani  路  6Comments

SimonSchick picture SimonSchick  路  6Comments

divramod picture divramod  路  5Comments

simonemazzoni picture simonemazzoni  路  4Comments

jefflewis picture jefflewis  路  6Comments