-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (34 loc) · 817 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
const { EventEmitter } = require("events");
const ticker = (time, cb) => {
const emitter = new EventEmitter();
let ticks = 0;
process.nextTick(() => emitter.emit("start"));
if (time % 5 === 0) {
emitter.emit("error", "Dividing by 5");
return
}
const interval = setInterval(() => {
if (Date.now() % 5 === 0) {
emitter.emit("error", "Dividing by 5");
}
emitter.emit("tick");
ticks++;
}, 50);
setTimeout(() => {
clearInterval(interval);
cb(ticks);
}, time);
return emitter;
};
const emitter = ticker(324, (ticks) => {
console.log("total number of ticks ", ticks);
});
emitter.on("tick", () => {
console.log("tick");
});
emitter.on("start", () => {
console.log("start called");
});
emitter.on("error", (e) => {
console.error("Error happened", e);
});