-
Notifications
You must be signed in to change notification settings - Fork 41
Open
Description
func FanIn(futures ...*Future) *Future {
f := NewFuture()
go func() {
fanResults := make([]*FanResult, len(futures))
wg := new(sync.WaitGroup)
wg.Add(len(futures))
// 协程捕获到的 f 是错误的,只会是最后一个
for i, f := range futures {
go func(idx int) {
defer wg.Done()
ret, err := f.Get()
fanResults[idx] = &FanResult{Result: ret, Err: err}
}(i)
}
wg.Wait()
f.Set(fanResults, nil)
}()
return f
}
修正版:
func FanIn(futures ...*Future) *Future {
f := NewFuture()
go func() {
fanResults := make([]*FanResult, len(futures))
wg := new(sync.WaitGroup)
wg.Add(len(futures))
// 协程捕获到的 f 是错误的,只会是最后一个
for i, f := range futures {
go func(idx int, f *Future) {
defer wg.Done()
ret, err := f.Get()
fanResults[idx] = &FanResult{Result: ret, Err: err}
}(i, f)
}
wg.Wait()
f.Set(fanResults, nil)
}()
SDK版本:
v1.7.17
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels