Skip to content

Commit

Permalink
feat: add GetOrDefault for easy using
Browse files Browse the repository at this point in the history
  • Loading branch information
jizhuozhi committed Aug 1, 2024
1 parent 9ec417f commit 6529b0f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
8 changes: 8 additions & 0 deletions future.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ func (f *Future[T]) Get() (T, error) {
return f.state.get()
}

func (f *Future[T]) GetOrDefault(defaultVal T) T {
val, err := f.state.get()
if err != nil {
return defaultVal
}
return val
}

// noCopy may be embedded into structs which must not be copied
// after the first use.
//
Expand Down
1 change: 1 addition & 0 deletions future_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestPromiseAndFuture(t *testing.T) {
val, err := f.Get()
assert.Equal(t, val, 1)
assert.Equal(t, err, errFoo)
assert.Equal(t, 2, f.GetOrDefault(2))
}

func TestPromiseAndFutureConcurrency(t *testing.T) {
Expand Down

0 comments on commit 6529b0f

Please sign in to comment.