Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/artem328/go-promise
The promises API for Go
https://github.com/artem328/go-promise
Last synced: about 1 month ago
JSON representation
The promises API for Go
- Host: GitHub
- URL: https://github.com/artem328/go-promise
- Owner: artem328
- License: mit
- Created: 2023-12-23T16:00:06.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2023-12-26T06:01:46.000Z (almost 1 year ago)
- Last Synced: 2023-12-26T18:27:49.019Z (12 months ago)
- Language: Go
- Homepage:
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# GoPromise
[![Go Reference](https://pkg.go.dev/badge/github.com/artem328/go-promise.svg)](https://pkg.go.dev/github.com/artem328/go-promise)
[![Build](https://github.com/artem328/go-promise/actions/workflows/go.yml/badge.svg?branch=master)](https://github.com/artem328/go-promise/actions/workflows/go.yml)
[![codecov](https://codecov.io/gh/artem328/go-promise/graph/badge.svg)](https://codecov.io/gh/artem328/go-promise)The promises API for Go with:
- Promise chaining
- Panic handling
- Generic support## Installation
Add import to your go source code and that's it
```go
import "github.com/artem328/go-promise"
```## Example
```go
package mainimport (
"fmt"
"github.com/artem328/go-promise"
)func main() {
p := promise.New(func(resolve func(int), reject func(error)) {
var i int
// do something here
resolve(i)
}).
Then(func(i int) *promise.Promise[int] {
p, resolve, _ := promise.WithResolvers[int]()go func() {
var j int
// do something else
resolve(i + j)
}()return p
}).
Then(func(j int) *promise.Promise[int] {
// do something again
return promise.Resolve(j + 10)
}).
Catch(func(err error) *promise.Promise[int] {
// log error
}).
Finally(func() *promise.Promise[int] {
// all done (whether successfully or not)
})i, err := p.Await()
if err != nil {
panic(err)
}fmt.Println("result", i)
}```