-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
46 lines (41 loc) · 816 Bytes
/
exec.go
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
package signal
import (
"context"
)
func executeAndFulfill(f func() error, done chan<- error) {
err := f()
if err != nil {
done <- err
}
close(done)
}
// ExecuteAsync executes a function asychrously and return its result.
func ExecuteAsync(f func() error) <-chan error {
done := make(chan error, 1)
go executeAndFulfill(f, done)
return done
}
func ErrorOrFinish1(ctx context.Context, c <-chan error) error {
select {
case <-ctx.Done():
return ctx.Err()
case err := <-c:
return err
}
}
func ErrorOrFinish2(ctx context.Context, c1, c2 <-chan error) error {
select {
case <-ctx.Done():
return ctx.Err()
case err, failed := <-c1:
if failed {
return err
}
return ErrorOrFinish1(ctx, c2)
case err, failed := <-c2:
if failed {
return err
}
return ErrorOrFinish1(ctx, c1)
}
}