Skip to content

Index

func Await

go
func Await[T any](f *Future[T]) (T, error)

Await for a future object results.

func AwaitAll

go
func AwaitAll[T any](futures ...*Future[T]) ([]T, error)

AwaitAll is the same as Await, but for multiple futures.

func AwaitRuntime

go
func AwaitRuntime(f ImplementsAwaitRuntime) (any, error)

AwaitRuntime is a runtime version of async.Await.

func Filter

go
func Filter[T any](slice []T, fn func(v T) bool) []T

Filter returns filtered slice according to the given function. Same as slice.Filter, but executed in parallel. Useful for cases when comparison function becomes expensive.

Usage:

Filter(slice.Range(1, 1000), func(v int) bool { return v < 500 })

func Map

go
func Map[T1 any, T2 any](slice []T1, fn func(v T1) T2) []T2

Map returns a new slice with the results of applying the given function to each element in the given slice. Asynchronous version of slice.Map. Please note, it's not always faster! Goroutines allocation and synchronization have own cost.

Usage:

// Let's assume we have some workload in Workload function, which returns an integer.

results := async.Map(slice.Range(1, 1000), func(v int) int {
	return Workload(v)
})

func Pool

go
func Pool[T1 any, T2 any](num int, worker func(v T1) T2) (chan T1, chan T2)

Pool creates i/o channels and spins up a pool of workers for it. To stop the pool, you have to close input channel. Output channel will be closed automatically on workers completion.

Please note, output order is not guaranteed! Use own wrapper if you need to identify output.

Usage:

// Task holds processing data, like id, input, and result.
type Task struct {
	ID int
	Value int
	Result int
}

in, out := async.Pool(10, func(v *Task) *Task {
	return Workload(v)
})

go func() {
	for i := 0; i < 1000; i++ {
		in <- &Task{ID: i, Value: i}
	}
	close(in)
}()

for v := range out {
	fmt.Println(v.ID, v.Result)
}

type Future

Future is an execution result of an asynchronous function that returns immediately, without locking execution thread. To lock execution and wait for result, use .Await() method or async.Await() function. As an alternative you can use a syntax similar to JavaScript Promise, using .Then() and .Catch() methods.

Usage:

// Let's assume we have a future object in "ftr" variable.
// We can lock execution and wait for a result with .Await()
res, err := ftr.Await()
// Or, we can use async.Await()
res, err := async.Await(ftr)
// Or, we can avoid locking execution and provide then/catch
// functions to handle execution results.
ftr.Then(func(val string) {
	println(val)
}).Catch(func(err error) {
	println(err.Error())
})
go
type Future[T any] struct {
    // contains filtered or unexported fields
}

func New

go
func New[T any](fn func() (T, error)) *Future[T]

New runs a function in a goroutine and returns Future object for it.

func (*Future[T]) Await

go
func (f *Future[T]) Await() (T, error)

Await for a future object results.

Usage:

// Let's assume we have a future object in "ftr" variable.
res, err := ftr.Await()

func (*Future[T]) AwaitRuntime

go
func (f *Future[T]) AwaitRuntime() (any, error)

AwaitRuntime is a runtime version of .Await()

Usage:

// Let's assume we have a future object in "ftr" variable.
// Result will be stored as "any" type, so you'll need to cast it.
res, err := ftr.AwaitRuntime()

func (*Future[T]) Catch

go
func (f *Future[T]) Catch(fn func(error)) *Future[T]

Catch accepts a function, that will be executed on future execution error.

Usage:

// Let's assume we have a future object of string in "ftr" variable.
ftr.Catch(func(err error) {
	println(err.Error())
})

func (*Future[T]) MarshalJSON

go
func (f *Future[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements future marshalling.

func (*Future[T]) Then

go
func (f *Future[T]) Then(fn func(T)) *Future[T]

Then accepts a function, that will be executed on future work completion.

Usage:

// Let's assume we have a future object of string in "ftr" variable.
ftr.Then(func(v string) {
	println(v)
})

func (*Future[T]) UnmarshalJSON

go
func (f *Future[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements future unmarshalling.

type ImplementsAwaitRuntime

Interface for runtime await.

go
type ImplementsAwaitRuntime interface {
    AwaitRuntime() (any, error)
}

Generated by gomarkdoc