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 psql
This commit is contained in:
Barış Soner Uşaklı
2018-08-31 11:04:42 -04:00
committed by GitHub
parent e882a091a1
commit 0519f84734
13 changed files with 60 additions and 1 deletions

37
src/promisify.js Normal file
View File

@@ -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;
};