Promisify modules (#6723)
* WIP promisify * promisify psql * ability to skip some keys * dont promisify client object * remove async * clone entire module so it has all properties * add shim for node 6 * ignore sessionStore as well * ignore pool on psqlv1.18.x
parent
e882a091a1
commit
0519f84734
@ -0,0 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
// remove once node 6 support is removed
|
||||
require('util.promisify/shim')();
|
||||
|
||||
var util = require('util');
|
||||
var _ = require('lodash');
|
||||
|
||||
module.exports = function (theModule, ignoreKeys) {
|
||||
ignoreKeys = ignoreKeys || [];
|
||||
function isCallbackedFunction(func) {
|
||||
if (typeof func !== 'function') {
|
||||
return false;
|
||||
}
|
||||
var str = func.toString().split('\n')[0];
|
||||
return str.includes('callback)');
|
||||
}
|
||||
function promisifyRecursive(module) {
|
||||
if (!module) {
|
||||
return;
|
||||
}
|
||||
var keys = Object.keys(module);
|
||||
keys.forEach(function (key) {
|
||||
if (ignoreKeys.includes(key)) {
|
||||
return;
|
||||
}
|
||||
if (isCallbackedFunction(module[key])) {
|
||||
module[key] = util.promisify(module[key]);
|
||||
} else if (typeof module[key] === 'object') {
|
||||
promisifyRecursive(module[key]);
|
||||
}
|
||||
});
|
||||
}
|
||||
const asyncModule = _.cloneDeep(theModule);
|
||||
promisifyRecursive(asyncModule);
|
||||
return asyncModule;
|
||||
};
|
Loading…
Reference in New Issue