@@ -9,24 +9,31 @@ import (
99)
1010
1111// All executes the functions asynchronously until all functions have been finished. If some
12- // function returns an error or panic, it will return the error immediately and send a cancel
13- // signal to all other functions by context.
14- func All (funcs ... func (context.Context ) error ) error {
12+ // function returns an error or panic, it will immediately return the index of the function and the
13+ // error, and send a cancel signal to all other functions by context.
14+ //
15+ // The index of the function will be -1 if all functions have been completed without error or
16+ // panic.
17+ func All (funcs ... func (context.Context ) error ) (int , error ) {
1518 return all (context .Background (), funcs ... )
1619}
1720
1821// AllWithContext executes the functions asynchronously until all functions have been finished, or
1922// the context is done (canceled or timeout). If some function returns an error or panic, it will
20- // return the error immediately and send a cancel signal to all other functions by context.
21- func AllWithContext (ctx context.Context , funcs ... func (context.Context ) error ) error {
23+ // immediately return the index of the index and the error and send a cancel signal to all other
24+ // functions by context.
25+ //
26+ // The index of the function will be -1 if all functions have been completed without error or
27+ // 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 ) {
2229 return all (ctx , funcs ... )
2330}
2431
2532// all executes the functions asynchronously until all functions have been finished, or the context
2633// is done (canceled or timeout).
27- func all (parent context.Context , funcs ... func (context.Context ) error ) error {
34+ func all (parent context.Context , funcs ... func (context.Context ) error ) ( int , error ) {
2835 if len (funcs ) == 0 {
29- return nil
36+ return - 1 , nil
3037 }
3138
3239 if parent == nil {
@@ -36,11 +43,12 @@ func all(parent context.Context, funcs ...func(context.Context) error) error {
3643 ctx , canFunc := context .WithCancel (parent )
3744 defer canFunc ()
3845
39- errCh := make (chan error )
40- defer close (errCh )
46+ ch := make (chan executeResult )
47+ defer close (ch )
4148
4249 for i := 0 ; i < len (funcs ); i ++ {
4350 fn := funcs [i ]
51+ n := i
4452 go func () {
4553 childCtx , childCanFunc := context .WithCancel (ctx )
4654 defer childCanFunc ()
@@ -53,7 +61,10 @@ func all(parent context.Context, funcs ...func(context.Context) error) error {
5361 case <- ctx .Done ():
5462 return
5563 default :
56- errCh <- err
64+ ch <- executeResult {
65+ Error : err ,
66+ Index : n ,
67+ }
5768 }
5869 }()
5970 }
@@ -62,16 +73,16 @@ func all(parent context.Context, funcs ...func(context.Context) error) error {
6273 for finished < len (funcs ) {
6374 select {
6475 case <- parent .Done ():
65- return errors .New ("context canceled" )
66- case err := <- errCh :
67- if err != nil {
68- return err
76+ return - 1 , errors .New ("context canceled" )
77+ case ret := <- ch :
78+ if ret . Error != nil {
79+ return ret . Index , ret . Error
6980 }
7081 finished ++
7182 }
7283 }
7384
74- return nil
85+ return - 1 , nil
7586}
7687
7788// AllCompleted executes the functions asynchronously until all functions have been finished. It
0 commit comments