https://github.com/4strodev/promise
A simple go library to manage async operations using promises
https://github.com/4strodev/promise
Last synced: about 1 year ago
JSON representation
A simple go library to manage async operations using promises
- Host: GitHub
- URL: https://github.com/4strodev/promise
- Owner: 4strodev
- License: mit
- Created: 2023-09-18T15:27:49.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-10-06T11:54:45.000Z (over 2 years ago)
- Last Synced: 2025-04-24T05:10:00.223Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Go Promise package
A simple library that allows you to create promises with go.
## Installation
```sh
go get github.com/4strodev/promise
```
## Examples
Creating a promise
```go
package main
import (
"context"
"fmt"
"github.com/4strodev/promise/pkg"
"time"
)
func main() {
p := promise.New(func(resolve func(int), reject func(error)) {
time.Sleep(1 * time.Second)
resolve(1)
})
result, err := p.Await(context.Background())
if err != nil {
panic(err)
}
fmt.Println(result)
// Output: 1
}
```
Working with multiple promises
```go
package main
import (
"context"
"fmt"
"math/rand"
"time"
"github.com/4strodev/promise/pkg"
)
type Job struct {
Value1 int
Value2 int
}
func main() {
jobs := []Job{}
promises := []*promise.Promise[int]{}
for i := 0; i < 10; i++ {
job := Job{
Value1: rand.Intn(10),
Value2: rand.Intn(10),
}
jobs = append(jobs, job)
}
for _, _job := range jobs {
// For new go developers loop variables are loop scoped
// that means that the variable _job is declared only once
// and for each iteration the value is overrited
// that caused a lot of bugs and will be changed in go 1.22
// for more information see https://go.dev/blog/loopvar-preview
job := _job
p := promise.New(func(resolve func(int), reject func(error)) {
result := job.Value1 + job.Value2
resolve(result)
})
promises = append(promises, p)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
values, err := promise.MergeAll(ctx, promises...).Await(ctx)
if err != nil {
panic(err)
}
fmt.Println(values)
}
```
Chaining promises
```go
package main
import (
"time"
"strconv"
"context"
"github.com/4strodev/promise"
)
func main() {
p := promise.New(func(resolve func(int), reject func(error)) {
time.Sleep(time.Millisecond * 1)
resolve(1)
})
ctx := context.Background()
newPromise := promise.Then(ctx, p, func(num int) string {
time.Sleep(time.Millisecond * 1)
return strconv.Itoa(num * 2)
})
value, err := newPromise.Await(ctx)
if err != nil {
panic(err)
}
fmt.Println(value)
// Output: 2
}
```
## Suggestions are accepted
It does not mean that all suggestions will apply. It means that the suggestions will be
read and evaluated. Feel free to make any PR or Issue. Obviously, keep in mind that this is a personal project.
Which I made open source under the MIT license. I don't have much time, but I promise I will be
checking the repository 😉.