Skip to content

Commit

Permalink
feat: add Done for easy using
Browse files Browse the repository at this point in the history
  • Loading branch information
jizhuozhi committed Jul 31, 2024
1 parent 5cdbdad commit 9f0a570
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func Lazy[T any](f func() (T, error)) *Future[T] {
return &Future[T]{state: s}
}

func Done[T any](val T) *Future[T] {
s := &state[T]{}
s.set(val, nil)
return &Future[T]{state: s}
}

func Await[T any](f *Future[T]) (T, error) {
return f.Get()
}
Expand Down
7 changes: 7 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ func TestLazyConcurrency(t *testing.T) {
wg.Wait()
}

func TestDone(t *testing.T) {
f := Done(1)
val, err := f.Get()
assert.Equal(t, 1, val)
assert.Equal(t, nil, err)
}

func TestAwait(t *testing.T) {
f := Async(func() (int, error) {
return 1, nil
Expand Down

0 comments on commit 9f0a570

Please sign in to comment.