From 02b95b4ac3d8bf954047e10beab85973e03853b8 Mon Sep 17 00:00:00 2001 From: jizhuozhi Date: Sun, 11 Aug 2024 18:41:43 +0800 Subject: [PATCH] fix: return directly if no futures --- api.go | 4 ++++ api_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+) 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)