-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path31b-example.js
More file actions
43 lines (37 loc) · 795 Bytes
/
31b-example.js
File metadata and controls
43 lines (37 loc) · 795 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
// Works in very few browsers, e.g. Firefox 57
function wait(period) {
return new Promise(resolve => {
setTimeout(resolve, period);
});
}
async function* users(from, to) {
for (let x = from; x <= to; x++) {
const res = await fetch('http://jsonplaceholder.typicode.com/users/' + x);
const json = await res.json();
yield json;
}
}
function map(f) {
return async function*(source) {
for await (let x of source) {
yield f(x);
}
};
}
function filter(condition) {
return async function*(source) {
for await (let x of source) {
if (condition(x)) {
yield x;
}
}
};
}
async function main() {
for await (let x of filter(name => name[0] === 'M')(
map(u => u.name)(users(1, 10)),
)) {
console.log(x);
}
}
main();