diff --git a/api.go b/api.go index a85bd85..07443c6 100644 --- a/api.go +++ b/api.go @@ -70,6 +70,10 @@ type AnyResult[T any] struct { } func AnyOf[T any](fs ...*Future[T]) *Future[AnyResult[T]] { + if len(fs) == 0 { + return Done(AnyResult[T]{Index: -1}) + } + var counter int32 var done uint32 var errIndex int32 = -1 diff --git a/api_test.go b/api_test.go index c74fc48..578c44a 100644 --- a/api_test.go +++ b/api_test.go @@ -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)