https://github.com/jochasinga/go-promise
Light-weight channel-compatible Promise
https://github.com/jochasinga/go-promise
go golang promise
Last synced: 12 months ago
JSON representation
Light-weight channel-compatible Promise
- Host: GitHub
- URL: https://github.com/jochasinga/go-promise
- Owner: jochasinga
- License: mit
- Created: 2017-05-24T02:33:29.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-24T22:47:04.000Z (about 9 years ago)
- Last Synced: 2025-03-28T08:43:18.270Z (about 1 year ago)
- Topics: go, golang, promise
- Language: Go
- Size: 4.88 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-promise
Light-weight channel-compatible [Promise](https://promisesaplus.com/) implementation.
## Usage
Use a `Promise` to easily handle an async routine, like an HTTP request
```go
p := promise.New(func() (interface{}, error) {
return http.Get("www.google.com")
})
_ = p.Then(func(res interface{}) {
fmt.Println(res)
}, func(err error) {
fmt.Println(err)
})
```
Convert channels to Promise and vice versa
```go
rc := make(chan interface{})
go func() {
<-time.After(100)
rc <- "hello"
close(rc)
}()
p1 := promise.From(rc)
p1.Then(func(result interface{}) {
fmt.Print(result) // "hello"
})
p2 := promise.New(func() (interface{}, error) {
<-time.After(100)
return "hello", nil
})
rc, errc := p2.To()
select {
case <-errc:
case result := <-rc:
fmt.Println(result) // "hello"
}
```
Compose `*Promise.Resolve` and `*Promise.Reject` chains
```go
p := New(func() (interface{}, error) {
return "hello", nil
})
_ = p.Then(func(result interface{}) {
fmt.Println(result == "hello") // true
word := result.(string) + " world"
p.Resolve(word)
}, func(err error) {
fmt.Println(err == nil) // true
}).Then(func(result interface{}) {
fmt.Println(result == "hello world") // true
p.Reject("too bad")
}).Then(nil, func(err error) {
fmt.Println(err.Error() == "too bad") // true
})
```