Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/felix-kaestner/promise
Go Promise Implementation with support for Generics (requires Go v1.18+)
https://github.com/felix-kaestner/promise
go golang goroutine promise
Last synced: about 3 hours ago
JSON representation
Go Promise Implementation with support for Generics (requires Go v1.18+)
- Host: GitHub
- URL: https://github.com/felix-kaestner/promise
- Owner: felix-kaestner
- License: mit
- Created: 2022-03-28T12:40:04.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-10T01:12:46.000Z (5 months ago)
- Last Synced: 2024-06-20T16:37:23.861Z (5 months ago)
- Topics: go, golang, goroutine, promise
- Language: Go
- Homepage:
- Size: 43 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Promise
Go Promise Implementation with support for Generics (requires Go v1.18+).
Run async operations lazily in a separate goroutine on the fly.
## Features
* Easy interface for composing async operations
* Executes a function in a separate goroutine
* Error Handling using functions (also executed in a separate goroutine)
* Support for Generics (requires Go v1.18+)
* Promises are resolved **lazily**, upon a first call to `Await`, `AwaitOr`, `Then`, `OnSuccess` or `onFailure`
* Support for `promise.All` and `promise.Race` (equivalent to the JavaScript [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) object)## Quickstart
```go
package mainimport (
"log"
"net/http""github.com/felix-kaestner/promise"
)func main() {
// Create a new promise.
// In this example the http request is executed in a separate goroutine
p := promise.New(func() (*http.Response, error) {
return http.Get("https://jsonplaceholder.typicode.com/posts/1")
})
// Handle successful and failed operations in a separate goroutine
p.Then(func(res *http.Response) {
log.Printf("Status: %s", res.Status)
}, func(err error) {
log.Fatalln(err)
})// Handle only successful operations in a separate goroutine
p.onSuccess(func(res *http.Response) {
log.Printf("Status: %s", res.Status)
})// Handle only failed operations in a separate goroutine
p.onFailure(func(err error) {
log.Fatalln(err)
})// Await the promise.
// This blocks execution until the promise is resolved.
res, err := p.Await()
// Provide a default value (calls Await() internally).
res = p.AwaitOr(nil)// Use channels to select the awaited promise
select {
case <-p.Done():
res, err = p.Await() // returns immediately since the promise is already resolved
case <-time.After(5000 * time.Millisecond):
fmt.Println("Timeout")
}// Take multiple promises and wait for all of them to be finished
p1 := promise.New(func() (*http.Response, error) {
return http.Get("https://jsonplaceholder.typicode.com/posts/1")
})
p2 := promise.New(func() (*http.Response, error) {
return http.Get("https://jsonplaceholder.typicode.com/posts/2")
})
res, err := promise.All(p1, p2).Await()// Take multiple promises and wait until the first of them to is finished
p1 := promise.New(func() (*http.Response, error) {
return http.Get("https://jsonplaceholder.typicode.com/posts/3")
})
p2 := promise.New(func() (*http.Response, error) {
return http.Get("https://jsonplaceholder.typicode.com/posts/4")
})
res, err := promise.Race(p1, p2).Await()}
```## Installation
Install with the `go get` command:
```
$ go get -u github.com/felix-kaestner/promise
```## Contribute
All contributions in any form are welcome! 🙌🏻
Just use the [Issue](.github/ISSUE_TEMPLATE) and [Pull Request](.github/PULL_REQUEST_TEMPLATE) templates and I'll be happy to review your suggestions. 👍---
Released under the [MIT License](LICENSE).