-
Notifications
You must be signed in to change notification settings - Fork 0
/
series.go
48 lines (41 loc) · 1.32 KB
/
series.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
47
48
package async
import (
"context"
)
// Series runs the functions by order and returns the results when all functions are completed.
// Each one runs once the previous function has been completed. If any function panics or returns
// an error, no more functions are run and it will return immediately.
//
// async.Series(func () {
// // do first thing
// }, func () {
// // do second thing
// }/*, ...*/)
func Series(funcs ...AsyncFn) ([][]any, error) {
return series(context.Background(), funcs...)
}
// SeriesWithContext runs the functions by order with the specified context and returns the results
// when all functions are completed. Each one runs once the previous function has been completed.
// If any function panics or returns an error, no more functions are run and it will return
// immediately.
func SeriesWithContext(ctx context.Context, funcs ...AsyncFn) ([][]any, error) {
return series(ctx, funcs...)
}
// series runs the functions by the order.
func series(ctx context.Context, funcs ...AsyncFn) ([][]any, error) {
validateAsyncFuncs(funcs...)
ctx = getContext(ctx)
ret := make([][]any, len(funcs))
for i := 0; i < len(funcs); i++ {
fn := funcs[i]
out, err := invokeAsyncFn(fn, ctx, nil)
ret[i] = out
if err != nil {
return ret, &executionError{
index: i,
err: err,
}
}
}
return ret, nil
}