Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/robinbraemer/parallel
Tiny utility for gracefully running functions in parallel with multi-error returning.
https://github.com/robinbraemer/parallel
Last synced: 27 days ago
JSON representation
Tiny utility for gracefully running functions in parallel with multi-error returning.
- Host: GitHub
- URL: https://github.com/robinbraemer/parallel
- Owner: robinbraemer
- License: mit
- Created: 2021-08-16T13:39:13.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-15T17:51:41.000Z (11 months ago)
- Last Synced: 2024-06-20T09:17:36.809Z (5 months ago)
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# parallel
`parallel` is a tiny utility package for running functions in parallel with error handling.
You can very easily orchestrate highly complex function executions that run in parallel or ordered fashion,
combine both and get a single standard Go error back that supports functions from Go's errors package like
`errors.Is` and `errors.As`.## Install
```bash
go get -u github.com/robinbraemer/parallel
```## Example
Imagine you need to run multiple functions in parallel
but some of them in order. It would be quiet code heavy
to do so in a clean manner.Luckily you can use the `parallel` package to de-complicate this:
```go
var fns [][]Fn
// ...
err := Ordered(
Parallel( // A
Parallel(fns[0]...), // 1
Parallel(fns[1]...), // 2
Parallel(fns[2]...), // 3
),
Parallel(fns[3]...), // B
Ordered(
Parallel(fns[5]...), // 4
Parallel(fns[6]...), // 5
), // C
).Do()
```This is what happens:
- Ordered runs A, B, C in unparalleled order and returns the first error encountered
- `A` runs 3 slices of functions in parallel and block until all are finished
- after `A` is done `B` runs functions in parallel
- after `B` is done `C` runs functions in parallel
- Ordered returnsThat means `1-3` must complete before `A` returns and `B` is run and so forth.