Skip to content

Commit 42c6902

Browse files
committed
feat: add alias AsyncFn for task function.
1 parent f7ad211 commit 42c6902

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

all.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
//
1515
// The index of the function will be -1 if all functions have been completed without error or
1616
// panic.
17-
func All(funcs ...func(context.Context) error) (int, error) {
17+
func All(funcs ...AsyncFn) (int, error) {
1818
return all(context.Background(), funcs...)
1919
}
2020

@@ -25,13 +25,13 @@ func All(funcs ...func(context.Context) error) (int, error) {
2525
//
2626
// The index of the function will be -1 if all functions have been completed without error or
2727
// panic, or the context has been canceled (or timeout) before all functions finished.
28-
func AllWithContext(ctx context.Context, funcs ...func(context.Context) error) (int, error) {
28+
func AllWithContext(ctx context.Context, funcs ...AsyncFn) (int, error) {
2929
return all(ctx, funcs...)
3030
}
3131

3232
// all executes the functions asynchronously until all functions have been finished, or the context
3333
// is done (canceled or timeout).
34-
func all(parent context.Context, funcs ...func(context.Context) error) (int, error) {
34+
func all(parent context.Context, funcs ...AsyncFn) (int, error) {
3535
if len(funcs) == 0 {
3636
return -1, nil
3737
}
@@ -88,7 +88,7 @@ func all(parent context.Context, funcs ...func(context.Context) error) (int, err
8888
// AllCompleted executes the functions asynchronously until all functions have been finished. It
8989
// will return an error slice that is ordered by the functions order, and a boolean value to
9090
// indicate whether any functions return an error or panic.
91-
func AllCompleted(funcs ...func(context.Context) error) ([]error, bool) {
91+
func AllCompleted(funcs ...AsyncFn) ([]error, bool) {
9292
return allCompleted(context.Background(), funcs...)
9393
}
9494

@@ -98,7 +98,7 @@ func AllCompleted(funcs ...func(context.Context) error) ([]error, bool) {
9898
// error or panic.
9999
func AllCompletedWithContext(
100100
ctx context.Context,
101-
funcs ...func(context.Context) error,
101+
funcs ...AsyncFn,
102102
) ([]error, bool) {
103103
return allCompleted(ctx, funcs...)
104104
}
@@ -107,7 +107,7 @@ func AllCompletedWithContext(
107107
// the context is done (canceled or timeout).
108108
func allCompleted(
109109
parent context.Context,
110-
funcs ...func(context.Context) error,
110+
funcs ...AsyncFn,
111111
) (errs []error, hasError bool) {
112112
hasError = false
113113
errs = make([]error, len(funcs))

all_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestAllSuccess(t *testing.T) {
2121
a := assert.New(t)
2222

2323
data := make([]bool, 5)
24-
funcs := make([]func(context.Context) error, 0, 5)
24+
funcs := make([]AsyncFn, 0, 5)
2525
for i := 0; i < 5; i++ {
2626
n := i
2727
funcs = append(funcs, func(ctx context.Context) error {
@@ -41,7 +41,7 @@ func TestAllFailure(t *testing.T) {
4141
a := assert.New(t)
4242

4343
data := make([]bool, 5)
44-
funcs := make([]func(context.Context) error, 0, 5)
44+
funcs := make([]AsyncFn, 0, 5)
4545
for i := 0; i < 5; i++ {
4646
n := i
4747
funcs = append(funcs, func(ctx context.Context) error {
@@ -77,7 +77,7 @@ func TestAllWithTimeoutContext(t *testing.T) {
7777
a := assert.New(t)
7878

7979
data := make([]bool, 5)
80-
funcs := make([]func(context.Context) error, 0, 5)
80+
funcs := make([]AsyncFn, 0, 5)
8181
for i := 0; i < 5; i++ {
8282
n := i
8383
funcs = append(funcs, func(ctx context.Context) error {
@@ -109,7 +109,7 @@ func TestAllCompletedSuccess(t *testing.T) {
109109
a := assert.New(t)
110110

111111
data := make([]bool, 5)
112-
funcs := make([]func(context.Context) error, 0, 5)
112+
funcs := make([]AsyncFn, 0, 5)
113113
for i := 0; i < 5; i++ {
114114
n := i
115115
funcs = append(funcs, func(ctx context.Context) error {
@@ -131,7 +131,7 @@ func TestAllCompletedPartialFailure(t *testing.T) {
131131
errNIs2 := errors.New("n = 2")
132132

133133
data := make([]bool, 5)
134-
funcs := make([]func(context.Context) error, 0, 5)
134+
funcs := make([]AsyncFn, 0, 5)
135135
for i := 0; i < 5; i++ {
136136
n := i
137137
funcs = append(funcs, func(ctx context.Context) error {
@@ -168,7 +168,7 @@ func TestAllCompletedWithTimeoutContext(t *testing.T) {
168168
errTimeout := errors.New("timeout")
169169

170170
data := make([]bool, 5)
171-
funcs := make([]func(context.Context) error, 0, 5)
171+
funcs := make([]AsyncFn, 0, 5)
172172
for i := 0; i < 5; i++ {
173173
n := i
174174
funcs = append(funcs, func(ctx context.Context) error {

result.go renamed to async.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package async
22

3+
import "context"
4+
5+
// AsyncFn is the function to run, it needs to accept a `context.Context` object and return an
6+
// error object.
7+
type AsyncFn func(context.Context) error
8+
39
// executeResult indicates the execution result whether the function returns an error or panic, and
410
// the index of the function in the parameters list.
511
type executeResult struct {

race.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ import (
1010
// Race executes the functions asynchronously, it will return the index and the result of the first
1111
// of the finished function (including panic), and it will not send a cancel signal to other
1212
// functions.
13-
func Race(funcs ...func(context.Context) error) (int, error) {
13+
func Race(funcs ...AsyncFn) (int, error) {
1414
return race(context.Background(), funcs...)
1515
}
1616

1717
// RaceWithContext executes the functions asynchronously, it will return the index and the result
1818
// of the first of the finished function (including panic), and it will not send a cancel signal
1919
// to other functions.
20-
func RaceWithContext(ctx context.Context, funcs ...func(context.Context) error) (int, error) {
20+
func RaceWithContext(ctx context.Context, funcs ...AsyncFn) (int, error) {
2121
return race(ctx, funcs...)
2222
}
2323

2424
// race executes the functions asynchronously, it will return the index and the result of the first
2525
// of the finished function (including panic).
26-
func race(ctx context.Context, funcs ...func(context.Context) error) (int, error) {
26+
func race(ctx context.Context, funcs ...AsyncFn) (int, error) {
2727
if len(funcs) == 0 {
2828
return -1, nil
2929
}

race_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestRace(t *testing.T) {
2121
a := assert.New(t)
2222

2323
data := make([]bool, 5)
24-
funcs := make([]func(context.Context) error, 0, 5)
24+
funcs := make([]AsyncFn, 0, 5)
2525
for i := 0; i < 5; i++ {
2626
n := i
2727
funcs = append(funcs, func(ctx context.Context) error {
@@ -56,7 +56,7 @@ func TestRaceWithContext(t *testing.T) {
5656
a := assert.New(t)
5757

5858
data := make([]bool, 5)
59-
funcs := make([]func(context.Context) error, 0, 5)
59+
funcs := make([]AsyncFn, 0, 5)
6060
for i := 0; i < 5; i++ {
6161
n := i
6262
funcs = append(funcs, func(ctx context.Context) error {

0 commit comments

Comments
 (0)