https://github.com/upbit/goalbatch
A simple way to execute functions asynchronously and waits for results
https://github.com/upbit/goalbatch
asynchronous batch coroutines golang
Last synced: 7 months ago
JSON representation
A simple way to execute functions asynchronously and waits for results
- Host: GitHub
- URL: https://github.com/upbit/goalbatch
- Owner: upbit
- License: mit
- Created: 2020-03-07T05:39:46.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-10T02:00:05.000Z (about 6 years ago)
- Last Synced: 2025-01-30T01:44:50.003Z (over 1 year ago)
- Topics: asynchronous, batch, coroutines, golang
- Language: Go
- Size: 12.7 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
goalbatch
================
[](https://github.com/upbit/goalbatch/actions?query=workflow%3Agoalbatch)
[](https://goreportcard.com/report/github.com/upbit/goalbatch)
[](https://codecov.io/gh/upbit/goalbatch)
[](http://godoc.org/github.com/upbit/goalbatch)
[](https://github.com/upbit/goalbatch/blob/master/LICENSE)
goalbatch - A simple way to execute functions asynchronously and waits for results
## Batch
> Batch method returns when all of the callbacks passed or context is done, returned responses and errors are ordered according to callback order
```go
timeout := time.Duration(100) * time.Millisecond
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
g := goalbatch.New(ctx)
rs, errs := g.Batch(
func(ctx context.Context) (interface{}, error) {
time.Sleep(5 * time.Second)
return 1, nil
},
func(ctx context.Context) (interface{}, error) {
return nil, errors.New("failed")
},
func(ctx context.Context) (interface{}, error) {
return 3, nil
},
func(ctx context.Context) (interface{}, error) {
return 4, nil
},
)
fmt.Println(rs)
fmt.Println(errs)
// Output:
// [ 3 4]
// [ "failed" ]
```
Or generate closure functions with parameters:
```go
newAsyncFunc := func(ctx context.Context, param1 string, param2 int) AsyncFunc {
return func(ctx context.Context) (interface{}, error) {
// deal with param1, param2...
result := fmt.Sprintf("p1=%s p2=%d", param1, param2)
return result, nil
}
}
fns := make([]goalbatch.AsyncFunc, 2)
fns[0] = newAsyncFunc(ctx, "foo", 1)
fns[1] = newAsyncFunc(ctx, "bar", 2)
g := goalbatch.New(ctx)
rs, errs := g.Batch(fns...)
fmt.Println(rs)
fmt.Println(errs)
// Output:
// ["p1=foo p2=1" "p1=bar p2=2"]
// [ ]
```