File tree Expand file tree Collapse file tree 2 files changed +18
-10
lines changed Expand file tree Collapse file tree 2 files changed +18
-10
lines changed Original file line number Diff line number Diff line change 66package async
77
88type Task func () interface {}
9- type Result chan interface {}
9+ type Channel chan interface {}
10+ type Result []interface {}
11+ type Out interface {}
1012
1113type Promise struct {
1214 //TODO:
1315 //Context - Cancel, Timeout
1416 //Concurrency vs Parallelism
1517 //What to do on error? Is it best way to abort all operation?
18+ //Can I use map for tasks? It will be easy to associate results with keys
1619}
1720
18- func (p * Promise ) All (tasks []Task ) [] Result {
19- workers := make ([]Result , 0 , len (tasks ))
21+ func (p * Promise ) All (tasks []Task ) Result {
22+ workers := make ([]Channel , 0 , len (tasks ))
2023 for _ , task := range tasks {
2124 result := execute (task )
2225 workers = append (workers , result )
2326 }
24- return workers
27+
28+ // gather data from all channels
29+ out := make (Result , 0 , len (tasks ))
30+ for _ , a := range workers {
31+ out = append (out , <- a )
32+ }
33+ return out
2534}
2635
27- func execute (task Task ) Result {
28- out := make (Result )
36+ func execute (task Task ) Channel {
37+ out := make (Channel )
2938 {
3039 go func () {
3140 defer close (out )
Original file line number Diff line number Diff line change @@ -22,10 +22,9 @@ func main() {
2222 },
2323 }
2424
25- for _ , a := range promise .All (tasks ) {
26- // switch statement for results
27- fmt .Println (<- a )
28- }
25+ result := promise .All (tasks )
26+
27+ fmt .Println ("Result: " , result )
2928
3029 fmt .Println ("exit" )
3130}
You can’t perform that action at this time.
0 commit comments