-
Notifications
You must be signed in to change notification settings - Fork 1
/
07-timeout.js
29 lines (23 loc) · 878 Bytes
/
07-timeout.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
import chan from '../../src'
import {p} from '../utils'
async function thread(index, chData, chTimeout) {
// On timeout, chTimeout will produce error, so the Promise returned
// from chan.select will get rejected, which, in turn, will cause
// the await operation to throw.
switch (await chan.select( chData, chTimeout )) {
case chData: return p(`thread ${index} got data:`, chData.value)
case chan.CLOSED: return p(`thread ${index}: chan ${chData} closed`)
}
}
function run() {
let chData = chan()
let chTimeout = chan.timeout(1000, `my bear got ill`)
thread(1, chData, chTimeout).catch(errorInThread(1))
thread(2, chData, chTimeout).catch(errorInThread(2))
thread(3, chData, chTimeout).catch(errorInThread(3))
chData.send('x')
}
function errorInThread(threadIndex) {
return err => p(`thread ${threadIndex} error: ${err.message}`)
}
run()