-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromisify.js
60 lines (53 loc) · 1.65 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// jshint multistr:true, evil:true
"use strict";
var murmurhash = require("./index");
module.exports = function (promise) {
if (!promise) {
promise = global.Promise;
}
if ("function" !== typeof promise) {
throw new Error("Promise constructor required");
}
var promisify = function (object) {
var value,
hash = {};
for (var name in object) {
if (object.hasOwnProperty(name)) {
if (!!(value = object[name])) {
switch (typeof value) {
case "function":
hash[name + "Async"] = wrap(promise, value);
break;
case "object":
hash[name] = promisify(value);
break;
}
}
}
}
return hash;
};
return promisify(murmurhash);
};
function wrap(promise, fn) {
return new Function(
"Promise",
"slice",
"fn",
"return function " +
fn.name +
"Async() { \
var cb, args = slice.call(arguments); \
var promise = new Promise(function(resolve, reject) { \
cb = function(err, res) { \
if (err) reject(err); else { \
resolve(res); \
} \
}; \
}); \
args.push(cb); \
fn.apply(this, args); \
return promise; \
}"
)(promise, Array.prototype.slice, fn);
}