Skip to content

Commit

Permalink
feat: add ToAny for easy using
Browse files Browse the repository at this point in the history
  • Loading branch information
jizhuozhi committed Jul 31, 2024
1 parent 9f0a570 commit 9ec417f
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 @@ -83,6 +83,12 @@ func AnyOf[T any](fs ...*Future[T]) *Future[AnyResult[T]] {
return &Future[AnyResult[T]]{state: s}
}

func ToAny[T any](f *Future[T]) *Future[any] {
return Then(f, func(val T, err error) (any, error) {
return val, err
})
}

func AllOf[T any](fs ...*Future[T]) *Future[struct{}] {
var done uint32
s := &state[struct{}]{}
Expand Down
10 changes: 10 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,16 @@ func TestAnyOfWhenErrFirst(t *testing.T) {
assert.Equal(t, errFoo, r.Err)
}

func TestToAny(t *testing.T) {
f := Async(func() (int, error) {
return 1, nil
})
ff := ToAny(f)
val, err := ff.Get()
assert.Equal(t, 1, val)
assert.Equal(t, nil, err)
}

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

0 comments on commit 9ec417f

Please sign in to comment.