Skip to content

Instantly share code, notes, and snippets.

@theladyjaye
Last active December 5, 2019 20:58
Show Gist options
  • Select an option

  • Save theladyjaye/8d8f3a093c929bf8d800e5827e4dd6ac to your computer and use it in GitHub Desktop.

Select an option

Save theladyjaye/8d8f3a093c929bf8d800e5827e4dd6ac to your computer and use it in GitHub Desktop.

Revisions

  1. Adam Venturella revised this gist Dec 5, 2019. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions test.go
    Original 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)
    }
  2. Adam Venturella revised this gist Dec 5, 2019. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions test.go
    Original file line number Diff line number Diff line change
    @@ -7,16 +7,14 @@ import (
    )

    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
    return "Finished", nil
    },
    )

    @@ -26,8 +24,8 @@ func TestAll(t *testing.T) {
    }

    result1 := operations.Value(0).(bool)
    result2 := operations.Value(1).(bool)
    result2 := operations.Value(1).(string)

    fmt.Printf("%v\n", result1)
    fmt.Printf("%v\n", result2)
    }
    }
  3. Adam Venturella revised this gist Dec 5, 2019. 2 changed files with 36 additions and 25 deletions.
    28 changes: 3 additions & 25 deletions async.go
    Original 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
    }

    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)
    }
    33 changes: 33 additions & 0 deletions test.go
    Original 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)
    }
  4. Adam Venturella created this gist Dec 5, 2019.
    102 changes: 102 additions & 0 deletions async.go
    Original 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)
    }