This repository was archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
108 lines (95 loc) · 2.7 KB
/
Copy pathindex.js
File metadata and controls
108 lines (95 loc) · 2.7 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// zero dependency, yeah!
module.exports = {
promisify,
run
};
// --------- Async Run Generator --------- //
// A simple method to run a generator method or instanced
// usage:
// run(mygen); // pass the function
// run(mygen()); // pass the instantiation of the generator
// returns:
// a promise;
function run(it){
var ret;
// if the "it" is a Function, then, run it (assume it is a generator).
if (it instanceof Function){
it = it();
}
return new Promise(function(resolve, reject){
function iterate(val){
try{
ret = it.next(val);
if (!ret.done){
// Note: To support only es2015 Promise we could have had "... instanceof Promise",
// but checking .then allows to support other Promise libraries, like Bluebird.
if (!isNull(ret.value) && ret.value.then instanceof Function){
// on success, just iterate
ret.value.then(v => {
iterate(v);
});
// on reject, we throw on the iterator
ret.value.catch(e => {
reject(e);
return;
});
}else{
// If we have a direct value, then, just iterate at the end of this event loop (i.e. after but in the same loop).
process.nextTick(function(){
iterate(ret.value);
});
}
}else{
// we resolve this master Promise
resolve(ret.value);
return;
}
}catch(err){
reject(err);
return;
}
}
iterate();
}).catch(e => {
console.log("ERROR run ", e, e.stack);
// TODO: needs to undertand this better.
it.throw(e);
});
}
// --------- /Async Run Generator --------- //
// --------- Promisify --------- //
// A minimalistic promisify method.
// - Avoiding .bind() as some perf tests performance issues -even when running the bound method-.
// - Using only ES2015 Promise (might not be as performant as bluebird, but there is value to minimize dependencies)
function promisify(fn, ctx){
return function(){
var args = arguments;
return new Promise(function(resolve, reject){
function callback(err, result){
if (err){
reject(new Error(err));
}else{
resolve(result);
}
}
try{
// add the callback method to the arguments
// see http://stackoverflow.com/a/13611033/686724 (in short, no need to shallow copy the arguments)
Array.prototype.push.call(args, callback);
// execute the function with the new callback
fn.apply(ctx,args);
}catch(e){
console.log("ERROR - promisify method run error", e);
reject(e);
}
});
};
}
// --------- /Promisify --------- //
// --------- Object Utils --------- //
var UD = "undefined";
// return true if value is null or undefined
function isNull(v){
return (typeof v === UD || v === null);
}
// --------- /Object Utils --------- //