@@ -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
1535func 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
93125func AllCompleted (funcs ... AsyncFn ) ([][]any , error ) {
94126 return allCompleted (context .Background (), funcs ... )
95127}
0 commit comments