-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathpromiseProxy.js
37 lines (30 loc) · 1.08 KB
/
promiseProxy.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
//for all the functions with callbacks that don't follow function(error, result) format... (which unfortunately there are a bunch)
function promisifyHelper(resolve, reject){
return function(err, result){
if(err){
return reject(err);
}else{
return resolve(result);
}
}
}
let promiseProxy = function(functionToProxy, firstResolve = true){
return new Proxy(functionToProxy, {
apply : function(target, thisArg, argumentList){
if(target.length === argumentList.length){
// console.log("FUNCTION IS CALLING!", functionToProxy);
target.bind(thisArg)(...argumentList);
}
else{
return new Promise((resolve, reject) => {
if(firstResolve) {
target.bind(thisArg)(...argumentList, resolve);
}else{
target.bind(thisArg)(...argumentList, promisifyHelper(resolve, reject));
}
})
}
}
})
};
module.exports = promiseProxy;