-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
59 lines (51 loc) · 1.51 KB
/
test.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
let assert = require('assert')
let delay = ms => new Promise(resolve => setTimeout(resolve, ms))
function pipe(...streams) {
let i = 0;
return streams.reduce((last, next) => {
console.log('reduction', ++i, last, next.name);
let res = next(last)
return res;
}, null)
}
function words() {
console.log("running words");
return ['foo', 'bar', 'baz']
}
async function* delayer(items) {
console.log("running delayer");
for (let item of items) {
await delay(500 + Math.floor(Math.random() * 1000));
console.log("delayer yielding item", item);
yield item;
}
}
async function* upper(iter) {
console.log("running upper");
for await (let chunk of iter) {
console.log("upper got a chunk");
yield chunk.toUpperCase();
}
}
async function concat(iter) {
const chunks = []
for await (let chunk of iter) {
console.log("concat got a chunk");
chunks.push(chunk)
}
return chunks.join()
}
async function main() {
/**
* For awhile I had just this and was wondering why the delayer and upper
* functions weren't being called. It's because the iterators were only
* being created, but never kicked off. If nothing ever calls `next` on the
* iterator, or if no for-of loop uses the iterator, then the function
* never starts. Be sure the final function in the series returns a Promise
*/
// await pipe(words, delayer, upper);
return await pipe(words, delayer, upper, concat);
}
main()
.then(res => console.log("done", res))
.catch(err => console.warn("Uh oh", err))