Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/artem328/go-promise

The promises API for Go
https://github.com/artem328/go-promise

Last synced: about 1 month ago
JSON representation

The promises API for Go

Awesome Lists containing this project

README

        

# GoPromise

[![Go Reference](https://pkg.go.dev/badge/github.com/artem328/go-promise.svg)](https://pkg.go.dev/github.com/artem328/go-promise)
[![Build](https://github.com/artem328/go-promise/actions/workflows/go.yml/badge.svg?branch=master)](https://github.com/artem328/go-promise/actions/workflows/go.yml)
[![codecov](https://codecov.io/gh/artem328/go-promise/graph/badge.svg)](https://codecov.io/gh/artem328/go-promise)

The promises API for Go with:
- Promise chaining
- Panic handling
- Generic support

## Installation

Add import to your go source code and that's it
```go
import "github.com/artem328/go-promise"
```

## Example

```go
package main

import (
"fmt"
"github.com/artem328/go-promise"
)

func main() {
p := promise.New(func(resolve func(int), reject func(error)) {
var i int
// do something here
resolve(i)
}).
Then(func(i int) *promise.Promise[int] {
p, resolve, _ := promise.WithResolvers[int]()

go func() {
var j int
// do something else
resolve(i + j)
}()

return p
}).
Then(func(j int) *promise.Promise[int] {
// do something again
return promise.Resolve(j + 10)
}).
Catch(func(err error) *promise.Promise[int] {
// log error
}).
Finally(func() *promise.Promise[int] {
// all done (whether successfully or not)
})

i, err := p.Await()
if err != nil {
panic(err)
}

fmt.Println("result", i)
}

```