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

Appearance settings

Commit 755bf78

Browse filesBrowse files
committed
Optimize examples
1 parent 44a6c6b commit 755bf78
Copy full SHA for 755bf78

File tree

Expand file treeCollapse file tree

2 files changed

+13
-12
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+13
-12
lines changed

‎JavaScript/1-function.js

Copy file name to clipboardExpand all lines: JavaScript/1-function.js
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ const randomChar = () => String
55

66
const subscribe = observer => {
77
const observable = { observer };
8-
const timer = setInterval(() => {
8+
setInterval(() => {
99
const char = randomChar();
1010
observer(char);
1111
}, 200);
12-
observable.complete = () => clearInterval(timer);
1312
return observable;
1413
};
1514

@@ -22,7 +21,9 @@ function observer(char) {
2221
process.stdout.write(char);
2322
count++;
2423
if (count > 50) {
25-
observable.complete();
2624
process.stdout.write('\n');
25+
process.exit(0);
2726
}
2827
}
28+
29+
console.dir({ observer, observable });

‎JavaScript/2-class.js

Copy file name to clipboardExpand all lines: JavaScript/2-class.js
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@ const randomChar = () => String
55

66
class Observable {
77
constructor() {
8-
this.timer = setInterval(() => {
9-
if (!this.onData) return;
8+
this.observer = null;
9+
setInterval(() => {
10+
if (!this.observer) return;
1011
const char = randomChar();
11-
this.onData(char);
12+
this.observer(char);
1213
}, 200);
1314
}
14-
subscribe(onData) {
15-
this.onData = onData;
15+
subscribe(observer) {
16+
this.observer = observer;
1617
return this;
1718
}
18-
complete() {
19-
clearInterval(this.timer);
20-
}
2119
}
2220

2321
// Usage
@@ -30,7 +28,9 @@ function observer(char) {
3028
process.stdout.write(char);
3129
count++;
3230
if (count > 50) {
33-
observable.complete();
3431
process.stdout.write('\n');
32+
process.exit(0);
3533
}
3634
}
35+
36+
console.dir({ observer, observable });

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.