-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1-function.js
More file actions
34 lines (29 loc) · 687 Bytes
/
1-function.js
File metadata and controls
34 lines (29 loc) · 687 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
'use strict';
const randomChar = () =>
String.fromCharCode(Math.floor(Math.random() * 25 + 97));
const subscribe = (observer) => {
let timer = setInterval(() => {
const char = randomChar();
observer(char);
}, 200);
const unsubscribe = () => {
if (!timer) return;
clearInterval(timer);
timer = null;
};
return { unsubscribe };
};
const createObserver = () => {
let count = 0;
return (char) => {
process.stdout.write(char);
count++;
if (count > 50) {
process.stdout.write('\n');
process.exit(0);
}
};
};
const observer = createObserver();
const observable = subscribe(observer);
console.dir({ observer, observable });