Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vloldik/gomise
Go promise
https://github.com/vloldik/gomise
go promise
Last synced: about 2 months ago
JSON representation
Go promise
- Host: GitHub
- URL: https://github.com/vloldik/gomise
- Owner: vloldik
- License: mit
- Created: 2024-11-17T17:04:43.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2024-11-17T17:21:00.000Z (about 2 months ago)
- Last Synced: 2024-11-17T18:29:03.162Z (about 2 months ago)
- Topics: go, promise
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Gomise - Go Promise Implementation
Gomise is a type-safe Promise implementation for Go that provides asynchronous programming capabilities with context support. It offers features similar to JavaScript Promises but with Go's type safety and context-aware concurrency patterns.
## Features
- Generic type-safe promises
- Context-aware promise execution
- Promise groups for sequential execution
- Custom context support with data passing
- Cancellation support
- Error handling with type safety## Installation
```bash
go get github.com/vloldik/gomise
```## Usage
### Basic Promise
```go
import (
"context"
"github.com/vloldik/gomise"
)// Create a simple promise that resolves with a string
promise := gomise.NewPromise[string](context.Background(), func(ctx interfaces.IPromiseContext, resolve interfaces.FnResolve, reject interfaces.FnReject) {
// Async work here
resolve("Hello, World!")
})// Await the result
result, err := promise.Await(context.Background())
if err != nil {
// Handle error
}
fmt.Println(result) // "Hello, World!"
```### Promise with Data Context
```go
type MyData struct {
Value string
}// Create a promise with custom data context
promise := gomise.NewPromiseWithConstructor[string, *gomise.DataContext[MyData]](
context.Background(),
gomise.NewDataContext[MyData],
func(ctx *gomise.DataContext[MyData], resolve interfaces.FnResolve, reject interfaces.FnReject) {
ctx.Data = MyData{Value: "Hello"}
resolve(ctx.Data.Value)
},
)
```### Promise Groups
```go
// Create a promise group for sequential execution
group := gomise.NewDefaultPromiseGroup[string](
func(ctx interfaces.IPromiseContext, resolve interfaces.FnResolve, reject interfaces.FnReject) {
resolve("Step 1")
},
func(ctx interfaces.IPromiseContext, resolve interfaces.FnResolve, reject interfaces.FnReject) {
resolve("Step 2")
},
)// Execute the group
promise := group.Execute(context.Background())
result, err := promise.Await(context.Background())
```## Error Handling
Promises can be rejected with errors:
```go
promise := gomise.NewPromise[string](context.Background(), func(ctx interfaces.IPromiseContext, resolve interfaces.FnResolve, reject interfaces.FnReject) {
reject(errors.New("something went wrong"))
})result, err := promise.Await(context.Background())
if err != nil {
// Handle error
}
```## Context Cancellation
Promises respect context cancellation:
```go
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()promise := gomise.NewPromise[string](ctx, func(ctx interfaces.IPromiseContext, resolve interfaces.FnResolve, reject interfaces.FnReject) {
time.Sleep(2 * time.Second) // Will be cancelled
resolve("Too late")
})_, err := promise.Await(ctx)
// err will be context.DeadlineExceeded
```## License
This project is open source and available under the [MIT License](LICENSE).