v2 / vlib / x / async / examples / timeout.v
23 lines · 21 sloc · 405 bytes · 15fb60b77ea6073658aa8355b247f2e1ae03b714
Raw
1import context
2import time
3import x.async as xasync
4
5fn main() {
6 xasync.with_timeout(20 * time.millisecond, fn (mut ctx context.Context) ! {
7 done := ctx.done()
8 select {
9 _ := <-done {
10 return ctx.err()
11 }
12 1 * time.second {
13 return error('work unexpectedly finished')
14 }
15 }
16 }) or {
17 println('timeout result: ${err.msg()}')
18 return
19 }
20
21 eprintln('expected timeout error')
22 exit(1)
23}
24