Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rexskz/gromise
A library to execute goroutines like Promise.allSettled in JavaScript with panic recover, make some scenarios (e.g. BFF data aggregation) easier.
https://github.com/rexskz/gromise
aggregation bff golang promise
Last synced: about 1 month ago
JSON representation
A library to execute goroutines like Promise.allSettled in JavaScript with panic recover, make some scenarios (e.g. BFF data aggregation) easier.
- Host: GitHub
- URL: https://github.com/rexskz/gromise
- Owner: RexSkz
- License: mit
- Created: 2023-11-20T08:05:48.000Z (12 months ago)
- Default Branch: master
- Last Pushed: 2024-01-31T07:17:59.000Z (10 months ago)
- Last Synced: 2024-05-01T16:43:21.474Z (7 months ago)
- Topics: aggregation, bff, golang, promise
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gromise
A library to execute goroutines like `Promise.allSettled` in JavaScript, make some scenarios (e.g. BFF data aggregation) easier.
## Usage
```go
import (
"errors"
"github.com/rexskz/gromise"
)// 1. define a list of functions
fns := []gromise.Executor{
func() (interface{}, error) {
// business logic that may cause lots of time
return data, nil
},
func() (interface{}, error) {
// a function that can return error
return nil, errors.New("this is an error")
},
func() (interface{}, error) {
// panic will be converted to error
panic("fatal error")
},
// ...other functions
}// 2. execute all functions concurrently and block until all
// functions are finished, throw error, panic, or timeout
timeoutMs := 1000
results, err := gromise.New(timeoutMs).AllSettled(fns).Await()// 3. the results (if not timeout) will be like:
assert.Equal(t, results, []*gromise.AllSettledValue{
{
Status: gromise.StatusFulfilled,
Value: data,
},
{
Status: gromise.StatusRejected,
Reason: errors.New("this is an error"),
},
{
Status: gromise.StatusRejected,
Reason: errors.New("fatal error"),
},
})// 4. if timeout, the error will be:
assert.Equal(t, err, gromise.ErrTimeout)
```## Test & Coverage
```bash
./scripts/run_tests.sh
```You can find the coverage report in `./coverage/index.html`.
## Why It's Useful
Sometimes we need to execute a list of functions concurrently and block until all functions are finished, e.g. BFF data aggregation. In JavaScript, we can use `Promise.all` to achieve this, but in Go, we have to use `sync.WaitGroup` or `chan`, and handle the `panic` manually, which is not so convenient. This library is to make this scenario easier.
## License
MIT