Skip to content

Lightweight Future abstraction over goroutines

License

Notifications You must be signed in to change notification settings

seprich/go-future

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-future

Tests Latest Version gocard

Introduction

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.

Install

go get github.com/seprich/go-future

Examples

import "github.com/seprich/go-future/async"

Basic use

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)
}

With Context

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")
	}
}

About

Lightweight Future abstraction over goroutines

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages