Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jizhuozhi/go-future
Promise/Future in Go
https://github.com/jizhuozhi/go-future
future go promise sync
Last synced: 26 days ago
JSON representation
Promise/Future in Go
- Host: GitHub
- URL: https://github.com/jizhuozhi/go-future
- Owner: jizhuozhi
- License: apache-2.0
- Created: 2024-07-12T07:08:51.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2024-11-04T03:04:56.000Z (2 months ago)
- Last Synced: 2024-11-04T04:17:44.906Z (2 months ago)
- Topics: future, go, promise, sync
- Language: Go
- Homepage:
- Size: 30.3 KB
- Stars: 35
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-future
[![codecov](https://codecov.io/github/jizhuozhi/go-future/graph/badge.svg?token=9UZDVRZCQM)](https://codecov.io/github/jizhuozhi/go-future)
[![goreport](https://goreportcard.com/badge/github.com/jizhuozhi/go-future)](https://goreportcard.com/badge/github.com/jizhuozhi/go-future)The `Promise` provides a facility to store a value or an error that is later acquired asynchronously via a `Future` created by the `Promise` object. Note that the `Promise` object is meant to be used only once.
Except for `Async`, all functions are event-driven, which means that no additional goroutines will be created to increase runtime scheduling overhead.
## Benchmark
```
goos: darwin
goarch: arm64
pkg: github.com/jizhuozhi/go-future
Benchmark
Benchmark/Promise
Benchmark/Promise-12 3053698 377.4 ns/op
Benchmark/WaitGroup
Benchmark/WaitGroup-12 2882623 424.9 ns/op
Benchmark/Channel
Benchmark/Channel-12 3004372 399.7 ns/op
PASS
```## Example
```go
package mainimport "github.com/jizhuozhi/go-future"
func main() {
p := future.NewPromise[string]()
go func() {
p.Set("foo", nil)
}()
f := p.Future()
val, err := f.Get()
println(val, err)
}
```## Useful API
### `Async`
Create an asynchronous task and return a `Future` to get the result.
### Example
```go
package mainimport "github.com/jizhuozhi/go-future"
func main() {
f := future.Async(func() (string, error) {
return "foo", nil
})
val, err := f.Get()
println(val, err)
}
```### `Lazy`
Create a lazy execution task and return a `Future` to get the result. The task will only be executed once, and the task will only be executed when `Get` is called for the first time.
### Example
```go
package mainimport "github.com/jizhuozhi/go-future"
func main() {
f := future.Lazy(func() (string, error) {
return "foo", nil
})
val, err := f.Get()
println(val, err)
}
```### `Then`
In reality, asynchronous tasks have dependencies. When task B depends on task A, it can be concatenated through `Then`.
### Example
```go
package mainimport (
"strconv""github.com/jizhuozhi/go-future"
)func main() {
f := future.Async(func() (int, error) {
return 1, nil
})
ff := future.Then(f, func(val int, err error) (string, error) {
if err != nil {
return "", err
}
return strconv.Itoa(val), nil
})
val, err := ff.Get()
println(val, err)
}
```