-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathall.go
35 lines (32 loc) · 830 Bytes
/
all.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
package flowmatic
import (
"context"
)
// All runs each task concurrently
// and waits for them all to finish.
// Each task receives a child context
// which is canceled once one task returns an error or panics.
// All returns nil if all tasks succeed.
// Otherwise,
// All returns a multierror containing the errors encountered.
// If a task panics during execution,
// a panic will be caught and rethrown in the parent Goroutine.
func All(ctx context.Context, tasks ...func(context.Context) error) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
return eachN(len(tasks), len(tasks), func(pos int) error {
defer func() {
panicVal := recover()
if panicVal != nil {
cancel()
panic(panicVal)
}
}()
err := tasks[pos](ctx)
if err != nil {
cancel()
return err
}
return nil
})
}