-
Notifications
You must be signed in to change notification settings - Fork 1
/
03-select-take.js
42 lines (36 loc) · 996 Bytes
/
03-select-take.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
import chan from '../../src'
import {p, sleep} from '../utils'
async function producer(name, ch, items) {
for (let item of items) {
p(`${ name }-> sending item: ${ item }...`)
await ch.send(item)
p(`${ name }-> done sending item: ${ item }`)
}
p(`${ name }-> all items sent, closing channel...`)
await ch.close()
p(`${ name }-> channel closed`)
}
async function consumer(ch1, ch2) {
while (true) {
p(`<- waiting for item...`)
switch (await chan.select(ch1, ch2)) {
case ch1:
p(`<- got item from ${ ch1.name }: ${ ch1.value }`)
break
case ch2:
p(`<- got item from ${ ch2.name }: ${ ch2.value }`)
break
case chan.CLOSED:
p(`<- all non-timeout chans closed`)
return
}
}
}
function run() {
let ch1 = chan().named('chan 1')
let ch2 = chan().named('chan 2')
producer('1', ch1, [ 'a', 'b' ]).catch(p)
producer('2', ch2, [ 'X', 'Y' ]).catch(p)
consumer(ch1, ch2).catch(p)
}
run()