Skip to content

Commit

Permalink
fix: return directly if no futures
Browse files Browse the repository at this point in the history
  • Loading branch information
jizhuozhi committed Aug 11, 2024
1 parent 6830e01 commit ee21123
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ type AnyResult[T any] struct {
}

func AnyOf[T any](fs ...*Future[T]) *Future[AnyResult[T]] {
if len(fs) == 0 {
s := &state[AnyResult[T]]{}
s.set(AnyResult[T]{Index: -1}, nil)
return &Future[AnyResult[T]]{state: s}
}

var counter int32
var done uint32
var errIndex int32 = -1
Expand Down
10 changes: 10 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ func TestAnyOf(t *testing.T) {
assert.Equal(t, nil, r.Err)
}

func TestAnyOfEmpty(t *testing.T) {
fs := make([]*Future[int], 0)
f := AnyOf(fs...)
r, err := f.Get()
assert.NoError(t, err)
assert.Equal(t, -1, r.Index)
assert.Zero(t, r.Val)
assert.NoError(t, r.Err)
}

func TestAnyOfWhenAllErr(t *testing.T) {
target := rand.Intn(10)
vals := make([]int, 10)
Expand Down

0 comments on commit ee21123

Please sign in to comment.