Skip to content

Commit

Permalink
feat: add Done2 for easy using
Browse files Browse the repository at this point in the history
  • Loading branch information
jizhuozhi committed Aug 11, 2024
1 parent b63afa0 commit e028ae9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ func Lazy[T any](f func() (T, error)) *Future[T] {
}

func Done[T any](val T) *Future[T] {
return Done2(val, nil)
}

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

Expand Down
7 changes: 7 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ func TestDone(t *testing.T) {
assert.Equal(t, nil, err)
}

func TestDone2(t *testing.T) {
f := Done2(1, errFoo)
val, err := f.Get()
assert.Equal(t, 1, val)
assert.Equal(t, errFoo, err)
}

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

0 comments on commit e028ae9

Please sign in to comment.