-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
59 lines (46 loc) · 919 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* co-gate.
*
*/
var Gate = function(){
// stack which store callback
this.stack = [];
// callback count
this.count = 0;
}
// create guard
Gate.prototype.in = function(){
var gate = this;
var cb = function(err, val){
// error
if(err)
return gate.next(err, null);
// count up
gate.count++;
// bind value to callback
cb.val = val;
// check finish
if(gate.count == gate.stack.length){
// create array to be yielded
var vals = [];
for(var i=0;i<gate.stack.length;i++){
vals.push(gate.stack[i].val)
}
if(vals.length == 1)
vals = vals[0];
if(gate.next)
gate.next(null, vals);
}
}
// store callback
gate.stack.push(cb);
return cb;
}
// get results
Gate.prototype.out = function(){
var gate = this;
return function(next){
gate.next = next;
}
}
module.exports = Gate;