Skip to content

Commit

Permalink
refactor: Async only set once to avoid panic in recover
Browse files Browse the repository at this point in the history
  • Loading branch information
jizhuozhi committed Jul 23, 2024
1 parent 203ab5b commit 0ab8114
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ var ErrTimeout = errors.New("future timeout")
func Async[T any](f func() (T, error)) *Future[T] {
s := &state[T]{}
go func() {
var val T
var err error
defer func() {
if r := recover(); r != nil {
err := fmt.Errorf("%w, err=%s, stack=%s", ErrPanic, r, debug.Stack())
var zero T
s.set(zero, err)
err = fmt.Errorf("%w, err=%s, stack=%s", ErrPanic, r, debug.Stack())
}
s.set(val, err)
}()
val, err := f()
s.set(val, err)
val, err = f()
}()
return &Future[T]{state: s}
}
Expand Down

0 comments on commit 0ab8114

Please sign in to comment.