Skip to content

Commit 31e4313

Browse files
committed
docs: add examples.
1 parent 451b939 commit 31e4313

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

all.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ import (
1212
//
1313
// The index of the function will be -1 if all functions have been completed without error or
1414
// panic.
15+
//
16+
// out, err := async.All(func() (int, error) {
17+
// return 1, nil
18+
// }, func() (string, error) {
19+
// time.Sleep(100 * time.Millisecond)
20+
// return "hello", nil
21+
// }, func(ctx context.Context) error {
22+
// time.Sleep(50 * time.Millisecond)
23+
// return nil
24+
// })
25+
// // out: [][]any{{1, nil}, {"hello", nil}, {nil}}
26+
// // err: nil
27+
//
28+
// _, err = async.All(func() (int, error) {
29+
// return 0, errors.New("some error")
30+
// }, func() (string, error) {
31+
// time.Sleep(100 * time.Millisecond)
32+
// return "hello", nil
33+
// })
34+
// // err: function 0 error: some error
1535
func All(funcs ...AsyncFn) ([][]any, error) {
1636
return all(context.Background(), funcs...)
1737
}
@@ -90,6 +110,18 @@ func runTaskInAll(ctx context.Context, n int, fn AsyncFn, ch chan<- executeResul
90110
// AllCompleted executes the functions asynchronously until all functions have been finished. It
91111
// will return an error slice that is ordered by the functions order, and a boolean value to
92112
// indicate whether any functions return an error or panic.
113+
//
114+
// out, err := async.AllCompleted(func() (int, error) {
115+
// return 1, nil
116+
// }, func() (string, error) {
117+
// time.Sleep(100 * time.Millisecond)
118+
// return "hello", nil
119+
// }, func(ctx context.Context) error {
120+
// time.Sleep(50 * time.Millisecond)
121+
// return errors.New("some error")
122+
// })
123+
// // out: [][]any{{1, nil}, {"hello", nil}, {some error}}
124+
// // err: function 2 error: some error
93125
func AllCompleted(funcs ...AsyncFn) ([][]any, error) {
94126
return allCompleted(context.Background(), funcs...)
95127
}

forever.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ type ForeverFn func(ctx context.Context, next func(context.Context)) error
1212
//
1313
// You can use the context and call the next function to pass values to the next invocation. The
1414
// next function can be invoked one time only, and it will have no effect if it is invoked again.
15+
//
16+
// err := Forever(func(ctx context.Context, next func(context.Context)) error {
17+
// v := ctx.Value("key")
18+
// if v != nil {
19+
// vi := v.(int)
20+
// if vi == 3 {
21+
// return errors.New("finish")
22+
// }
23+
//
24+
// fmt.Printf("value: %d\n", vi)
25+
//
26+
// next(context.WithValue(ctx, "key", vi+1))
27+
// } else {
28+
// next(context.WithValue(ctx, "key", 1))
29+
// }
30+
//
31+
// return nil
32+
// })
33+
// fmt.Printf("err: %v\n", err)
34+
// // value: 1
35+
// // value: 2
36+
// // err: finish
1537
func Forever(fn ForeverFn) error {
1638
return forever(context.Background(), fn)
1739
}

parallel.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ import (
1212
//
1313
// The number of concurrency must be greater than or equal to 0, and it means no concurrency
1414
// limitation if the number is 0.
15+
//
16+
// // Run 2 functions asynchronously at the time.
17+
// out, err := Parallel(2, func(ctx context.Context) (int, error) {
18+
// // Do something
19+
// return 1, nil
20+
// }, func(ctx context.Context) (string, error) {
21+
// // Do something
22+
// return "hello", nil
23+
// }, func(ctx context.Context) error {
24+
// // Do something
25+
// return nil
26+
// } /* , ... */)
27+
// // out: [][]any{{1, <nil>}, {"hello", <nil>}, {<nil>}}
28+
// // err: <nil>
1529
func Parallel(concurrency int, funcs ...AsyncFn) ([][]any, error) {
1630
return parallel(context.Background(), concurrency, funcs...)
1731
}

race.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ import (
88
// Race executes the functions asynchronously, it will return the index and the result of the first
99
// of the finished function (including panic), and it will not send a cancel signal to other
1010
// functions.
11+
//
12+
// out, index, err := Race(func(ctx context.Context) (int, error) {
13+
// request.Get("https://example.com")
14+
// return 0, nil
15+
// }, func(ctx context.Context) (string, error) {
16+
// time.Sleep(500 * time.Millisecond)
17+
// return "test", nil
18+
// })
19+
// // If the first function faster than the second one:
20+
// // out: []any{0, <nil>}, index: 0, err: <nil>
21+
// //
22+
// // Otherwise:
23+
// // out: []any{"test", <nil>}, index: 1, err: <nil>
1124
func Race(funcs ...AsyncFn) ([]any, int, error) {
1225
return race(context.Background(), funcs...)
1326
}

0 commit comments

Comments
 (0)