Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nkcmr/async
experimental promises in go1.18 with generics
https://github.com/nkcmr/async
async async-await generics go golang
Last synced: 2 months ago
JSON representation
experimental promises in go1.18 with generics
- Host: GitHub
- URL: https://github.com/nkcmr/async
- Owner: nkcmr
- License: mit
- Created: 2021-11-18T17:56:38.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-28T14:26:31.000Z (about 3 years ago)
- Last Synced: 2024-06-20T14:29:35.100Z (7 months ago)
- Topics: async, async-await, generics, go, golang
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 63
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license.txt
Awesome Lists containing this project
README
# async go
a prototype of "promises" in go1.18.
**note**: this is just an experiment used to test alternate patterns for dealing with asynchronous code in go. i would not recommend adopting this pattern blindly (especially in production environments) until there is a broader consensus in the go community about this. it might turn out that through some hands-on experience that go's native channels/goroutines are a good enough abstraction and there are no gains to be made from building on top of them.
## install
should be just a regular package:
```
go get -u -v code.nkcmr.net/async@latest
```## usage
promises abstract away a lot of details about how asynchronous work is handled.
so if you need for something to be async, simply us a promise:```go
import (
"context"
"code.nkcmr.net/async"
)type MyData struct {/* ... */}
func AsyncFetchData(ctx context.Context, dataID int64) async.Promise[MyData] {
return async.NewPromise(func() (MyData, error) {
/* ... */
return myDataFromRemoteServer, nil
})
}func DealWithData(ctx context.Context) {
myDataPromise := AsyncFetchData(ctx, 451)
// do other stuff while operation is not settled
// once your ready to wait for data:
myData, err := myDataPromise.Await(ctx)
if err != nil {/* ... */}
}
```