Lightweight Future abstraction over goroutines and return value channels. The aim of this library is to introduce a simple model for running goroutines, and collecting results from them.
- Features
- Execute any result returning function as a goroutine and return immediately a Future, which will receive the result.
- Any panics that are not recovered on your worker function will be captured into the Future and re-released ("throws" panic) when you invoke the
.Await()
method of the future. - As goroutines cannot be forcefully cancelled, context.Context is not directly part of Future type, however the usage of Context is trivial to include seamlessly. See examples below.
- Not supported
- Higher level functions such as map, flatMap, etc. In golang functional programming is an anti-pattern and this library does not aim to bring any fp concepts to go.
go get github.com/seprich/go-future
import "github.com/seprich/go-future/async"
package main
import (
"fmt"
"github.com/seprich/go-future/async"
)
func main() {
f := async.NewFuture(func() (string, error) {
return "Hello From GoRoutine !", nil
})
res, err := f.Await()
if err != nil {
fmt.Printf("Error: %v", err)
}
fmt.Printf("Result: %s", res)
}
package main
import (
"fmt"
"context"
"github.com/seprich/go-future/async"
"time"
)
func cancelableAsyncJob(ctx context.Context, name string) (string, error) {
fmt.Println("Hello from " + name)
for range 100 {
select {
case <-ctx.Done():
return "", fmt.Errorf("job canceled")
default:
time.Sleep(10 * time.Millisecond)
}
}
return "Done", nil
}
func main() {
ctx, cancelFn := context.WithCancel(context.Background())
fut := async.NewFuture2(cancelableAsyncJob, ctx, "Hi!")
time.Sleep(10 * time.Millisecond)
cancelFn()
_, err := fut.Await()
if err == nil {
panic("error expected")
}
}