-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
38 lines (30 loc) · 820 Bytes
/
index.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
35
36
37
38
'use strict';
const { promisify } = require('util')
const functionBlackListMap =
Object.getOwnPropertyNames(Object.prototype)
.reduce(function (map, functionName) {
map[functionName] = true
return map
}, {});
function _promisifyAllFunctions (object) {
for (const key of Object.getOwnPropertyNames(object)) {
if (functionBlackListMap[key]) {
continue;
}
const descriptor = Object.getOwnPropertyDescriptor(object, key)
if (!descriptor.get) {
const func = object[key]
if (typeof func === 'function') {
object[`${key}Async`] = promisify(func)
}
}
}
}
module.exports = function (object) {
_promisifyAllFunctions(object)
const proto = Object.getPrototypeOf(object)
if (proto) {
_promisifyAllFunctions(proto)
}
return object
}