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!
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?
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
promisifyAllso it detects that a function is passed and dives into theprototypein that case? Or would it introduce ambiguity?