future

package module
v8.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-future

codecov goreport

The Promise provides a facility to store a value or an error that is later acquired asynchronously via a Future created by the Promise object. Note that the Promise object is meant to be used only once.

Except for Async, all functions are event-driven, which means that no additional goroutines will be created to increase runtime scheduling overhead.

Before start

What I did was changed generic to the god-damn interface{}

Benchmark

goos: darwin
goarch: arm64
pkg: github.com/jizhuozhi/go-future
Benchmark
Benchmark/Promise
Benchmark/Promise-12         	 3053698	       377.4 ns/op
Benchmark/WaitGroup
Benchmark/WaitGroup-12       	 2882623	       424.9 ns/op
Benchmark/Channel
Benchmark/Channel-12         	 3004372	       399.7 ns/op
PASS

Example

package main

import "github.com/jizhuozhi/go-future"

func main() {
	p := future.NewPromise[string]()
	go func() {
		p.Set("foo", nil)
	}()
	f := p.Future()
	val, err := f.Get()
	println(val, err)
}

Useful API

Async

Create an asynchronous task and return a Future to get the result.

Example
package main

import "github.com/jizhuozhi/go-future"

func main() {
	f := future.Async(func() (string, error) {
		return "foo", nil
	})
	val, err := f.Get()
	println(val, err)
}
Lazy

Create a lazy execution task and return a Future to get the result. The task will only be executed once, and the task will only be executed when Get is called for the first time.

Example
package main

import "github.com/jizhuozhi/go-future"

func main() {
	f := future.Lazy(func() (string, error) {
		return "foo", nil
	})
	val, err := f.Get()
	println(val, err)
}
Then

In reality, asynchronous tasks have dependencies. When task B depends on task A, it can be concatenated through Then.

Example
package main

import (
	"strconv"

	"github.com/jizhuozhi/go-future"
)

func main() {
	f := future.Async(func() (int, error) {
		return 1, nil
	})
	ff := future.Then(f, func(val int, err error) (string, error) {
		if err != nil {
			return "", err
		}
		return strconv.Itoa(val), nil
	})
	val, err := ff.Get()
	println(val, err)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrPanic = errors.New("async panic")
View Source
var ErrTimeout = errors.New("future timeout")

Functions

func Await

func Await(f *Future) (interface{}, error)

Types

type AnyResult

type AnyResult struct {
	Index int
	Val   interface{}
	Err   error
}

type Future

type Future struct {
	// contains filtered or unexported fields
}

func AllOf

func AllOf(fs ...*Future) *Future

func AnyOf

func AnyOf(fs ...*Future) *Future

func Async

func Async(f func() (interface{}, error)) *Future

func Done

func Done(val interface{}) *Future

func Lazy

func Lazy(f func() (interface{}, error)) *Future

func Then

func Then(f *Future, cb func(interface{}, error) (interface{}, error)) *Future

func ThenAsync

func ThenAsync(f *Future, cb func(interface{}, error) *Future) *Future

func Timeout

func Timeout(f *Future, d time.Duration) *Future

func ToAny

func ToAny(f *Future) *Future

func Until

func Until(f *Future, t time.Time) *Future

func (*Future) Done

func (f *Future) Done() bool

func (*Future) Get

func (f *Future) Get() (interface{}, error)

func (*Future) GetOrDefault

func (f *Future) GetOrDefault(defaultVal interface{}) interface{}

func (*Future) Subscribe

func (f *Future) Subscribe(cb func(val interface{}, err error))

type Promise

type Promise struct {
	// contains filtered or unexported fields
}

func NewPromise

func NewPromise() *Promise

func (*Promise) Free

func (p *Promise) Free() bool

func (*Promise) Future

func (p *Promise) Future() *Future

func (*Promise) Set

func (p *Promise) Set(val interface{}, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL