forked from PerryTS/perry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_async_map.ts
More file actions
47 lines (37 loc) · 997 Bytes
/
test_async_map.ts
File metadata and controls
47 lines (37 loc) · 997 Bytes
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
import { EventEmitter } from 'events';
interface ProcessedEvent {
network: string;
timestamp: number;
}
class MyEmitter extends EventEmitter {
private network: string;
constructor(network: string) {
super();
this.network = network;
}
public async connect(): Promise<void> {
await new Promise(resolve => setTimeout(resolve, 10));
const event: ProcessedEvent = {
network: this.network,
timestamp: Date.now()
};
this.emit('event', event);
}
}
class Manager extends EventEmitter {
public async initialize(): Promise<void> {
const networks = ['ethereum'];
const promises = networks.map(async (network) => {
const connection = new MyEmitter(network);
connection.on('event', (event: ProcessedEvent) => {
this.emit('event', event);
});
await connection.connect();
});
await Promise.all(promises);
}
}
const manager = new Manager();
manager.initialize().then(() => {
console.log("Done");
});