Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eab859a

Browse files
authored
Create async-event-emitter.js
1 parent cefe9f8 commit eab859a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

‎async-event-emitter.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const EventEmitter = require("events");
2+
const fs = require("fs");
3+
const promisify = require("./promisify");
4+
5+
class WithTime extends EventEmitter {
6+
async execute(asyncFunc, ...args) {
7+
this.emit("begin");
8+
try {
9+
console.time("execute");
10+
const data = await asyncFunc(...args);
11+
this.emit("data", data);
12+
console.timeEnd("execute");
13+
this.emit("end");
14+
} catch (err) {
15+
this.emit("error", err);
16+
}
17+
}
18+
}
19+
20+
const readFileAsArray = async file => {
21+
try {
22+
const readFile = promisify(fs.readFile);
23+
const data = await readFile([file]);
24+
const lines = data
25+
.toString()
26+
.trim()
27+
.split("\n");
28+
return lines;
29+
} catch (error) {
30+
throw new Error(error);
31+
}
32+
};
33+
34+
const withTime = new WithTime();
35+
36+
withTime.on("begin", () => console.log("About to execute"));
37+
withTime.on("end", () => console.log("Done with execute"));
38+
withTime.on("data", data => {
39+
console.log(data);
40+
});
41+
withTime.on("error", error => {
42+
console.log(error);
43+
});
44+
45+
const filename = "test.txt";
46+
withTime.execute(readFileAsArray, filename);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /