Last active
December 5, 2019 20:58
-
-
Save theladyjaye/8d8f3a093c929bf8d800e5827e4dd6ac to your computer and use it in GitHub Desktop.
Revisions
-
Adam Venturella revised this gist
Dec 5, 2019 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,10 +12,16 @@ func TestAll(t *testing.T) { time.Sleep(time.Second * 1) return true, nil }, func() (interface{}, error) { time.Sleep(time.Second * 3) return "Finished", nil }, func() (interface{}, error) { <-time.After(time.Second * 3) return "Channel Finished", nil }, ) if operations.HasError() { @@ -25,7 +31,9 @@ func TestAll(t *testing.T) { result1 := operations.Value(0).(bool) result2 := operations.Value(1).(string) result3 := operations.Value(2).(string) fmt.Printf("%v\n", result1) fmt.Printf("%v\n", result2) fmt.Printf("%v\n", result3) } -
Adam Venturella revised this gist
Dec 5, 2019 . 1 changed file with 3 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,16 +7,14 @@ import ( ) func TestAll(t *testing.T) { operations := <-All( func() (interface{}, error) { time.Sleep(time.Second * 1) return true, nil }, func() (interface{}, error) { time.Sleep(time.Second * 3) return "Finished", nil }, ) @@ -26,8 +24,8 @@ func TestAll(t *testing.T) { } result1 := operations.Value(0).(bool) result2 := operations.Value(1).(string) fmt.Printf("%v\n", result1) fmt.Printf("%v\n", result2) } -
Adam Venturella revised this gist
Dec 5, 2019 . 2 changed files with 36 additions and 25 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,8 @@ package async import ( "sync" ) type AllResult struct { values []interface{} @@ -75,28 +78,3 @@ func All(actions ...func() (interface{}, error)) <-chan AllResult { return ch } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ package async import ( "fmt" "testing" "time" ) func TestAll(t *testing.T) { fmt.Println("Starting All") operations := <-All( func() (interface{}, error) { time.Sleep(time.Second * 1) return true, nil }, func() (interface{}, error) { time.Sleep(time.Second * 3) return false, nil }, ) if operations.HasError() { fmt.Println("Booo!") return } result1 := operations.Value(0).(bool) result2 := operations.Value(1).(bool) fmt.Printf("%v\n", result1) fmt.Printf("%v\n", result2) } -
Adam Venturella created this gist
Dec 5, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,102 @@ package async type AllResult struct { values []interface{} errors []error hasError bool } type AllError struct { Index int Error error } func (this AllResult) Value(index int) interface{} { return this.values[index] } func (this AllResult) Error(index int) error { return this.errors[index] } func (this AllResult) Errors() []AllError { var results []AllError for index, err := range this.errors { if err != nil { results = append(results, AllError{Index: index, Error: err}) } } return results } func (this AllResult) HasError() bool { return this.hasError } func allAction(index int, all *AllResult, action func() (interface{}, error), wg *sync.WaitGroup) { result, err := action() all.values[index] = result all.errors[index] = err if err != nil { all.hasError = true } wg.Done() } func All(actions ...func() (interface{}, error)) <-chan AllResult { ch := make(chan AllResult) go func(ch chan AllResult) { var wg sync.WaitGroup count := len(actions) allResult := AllResult{ values: make([]interface{}, count), errors: make([]error, count), } wg.Add(count) for index, each := range actions { go allAction(index, &allResult, each, &wg) } wg.Wait() ch <- allResult close(ch) }(ch) return ch } func TestAll(t *testing.T) { fmt.Println("Starting All") operations := <-All( func() (interface{}, error) { time.Sleep(time.Second * 1) return true, nil }, func() (interface{}, error) { time.Sleep(time.Second * 3) return false, nil }, ) if operations.HasError() { fmt.Println("Booo!") return } result1 := operations.Value(0).(bool) result2 := operations.Value(1).(bool) fmt.Printf("%v\n", result1) fmt.Printf("%v\n", result2) }