forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromisify.js
34 lines (32 loc) · 817 Bytes
/
promisify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
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;
};