async

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2023 License: MIT Imports: 5 Imported by: 0

README

Async

test Go Reference

Run async code with safe!

Example

package main

import (
	"fmt"

	"github.com/WinPooh32/async"
)

func main() {
	ch := async.Go(func(ch chan<- async.Option[string]) {
		ch <- async.MakeValue("Hello Async!")
	})

	value, err := async.Await(ch)
	if err != nil {
		panic(err)
	}

	fmt.Println(value)
}

Look for more examples at async_test.go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrChannelClosed = errors.New("channel is closed")

Functions

func Await

func Await[T any](ch <-chan Option[T]) (value T, err error)

Await reads channel ch and unwraps option to value and error.

func AwaitContext added in v1.1.0

func AwaitContext[T any](ctx context.Context, ch <-chan Option[T]) (value T, err error)

AwaitContext reads channel ch and unwraps option to value and error. Can be interrupted by closed context.

func Go

func Go[T any](f Func[T], capacity ...int) <-chan Option[T]

Go safely runs function f at a new goroutine. The ch channel will be closed automatically after f returns. If panic occurs inside of f it will be recovered and error will be written to the ch channel. If capacity is defined or greater than zero, buffered channel will be created.

func Group added in v1.2.0

func Group[T any](g func(i int) Func[T], n int, capacity ...int) <-chan Option[T]

Group runs g(i) functions in parallel, their output falls into one channel. n is a count of passed functions. i is ranged from 0 to n-1.

Types

type Func

type Func[T any] func(chan<- Option[T])

Func is a channel writer callback.

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}

Option is a wrapped pair of value and error.

func MakeErr

func MakeErr[T any](err error) Option[T]

MakeValue wraps error.

func MakeValue

func MakeValue[T any](v T) Option[T]

MakeValue wraps value.

func (Option[T]) Err

func (opt Option[T]) Err() error

Value unwraps opt's error.

func (Option[T]) Value

func (opt Option[T]) Value() T

Value unwraps opt's value.

Jump to

Keyboard shortcuts

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