v2 / vlib / x / async / examples / basic_group.v
30 lines · 26 sloc · 526 bytes · 15fb60b77ea6073658aa8355b247f2e1ae03b714
Raw
1import context
2import time
3import x.async as xasync
4
5fn main() {
6 mut group := xasync.new_group(context.background())
7
8 group.go(fn (mut ctx context.Context) ! {
9 _ = ctx
10 time.sleep(20 * time.millisecond)
11 return error('stop group')
12 })!
13
14 group.go(fn (mut ctx context.Context) ! {
15 done := ctx.done()
16 select {
17 _ := <-done {
18 return ctx.err()
19 }
20 1 * time.second {
21 return error('worker did not observe cancellation')
22 }
23 }
24 })!
25
26 group.wait() or {
27 println('group failed: ${err.msg()}')
28 return
29 }
30}
31