-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
40 lines (33 loc) · 893 Bytes
/
events.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
var EventEmitter = require('events').EventEmitter;
var getResource = function(c) {
var e = new EventEmitter();
process.nextTick(function() {
var count = 0;
e.emit('start');
var t = setInterval(function () {
e.emit('data', ++count);
if (count === 3)
{
e.emit('special', 'came from an event!');
}
if (count === c) {
e.emit('end', count);
clearInterval(t);
}
}, 10);
});
return(e);
};
var r = getResource(5);
r.on('start', function() {
console.log("I've started!");
});
r.on('data', function(d) {
console.log(" I received data -> " + d);
});
r.on('special', function(d) {
console.log(" I received data -> " + d);
});
r.on('end', function(t) {
console.log("I'm done, with " + t + " data events.");
});