-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (33 loc) · 822 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
import { Channel } from "https://cdn.skypack.dev/@eyalsh/async_channels@^1.0.0-beta4?dts";
const input = new Channel();
const evenOrOdd = ([x]) => {
const n = parseInt(x);
if (Number.isNaN(n) || x !== String(n)) return "notNumber";
return n % 2 === 0 ? "even" : "odd";
};
const {
even,
odd,
notNumber,
} = input.subscribe(evenOrOdd, ["even", "odd", "notNumber"]);
notNumber.forEach(([n, ackCh]) => {
alert(`"${n}" is not a number`);
ackCh.close();
});
even.forEach(([n, ackCh]) => {
alert(`${n} is even`);
ackCh.close();
});
odd.forEach(([n, ackCh]) => {
alert(`${n} is odd`);
ackCh.close();
});
while (true) {
const num = prompt("number: ");
if (num === null) break;
const ackCh = new Channel();
await input.send([num, ackCh]);
await ackCh.get();
}
input.close();
alert("Goodbye");