async

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2023 License: MIT Imports: 5 Imported by: 0

README

go-async

test Go Report Card codecov Version Badge License Badge Go Reference

A tool collection that provided asynchronous workflow control utilities, inspired by Promise in the Javascript.

Installation

go get -u github.com/ghosind/go-async

Getting Started

We provided the All function to execute all the functions asynchronously. It'll return -1 and a nil error if all functions are completed and no error return or panic. If some function returns an error or panic, it'll immediately return the index of the function and the error and send the cancel signal to all other functions.

index, err := async.All(func (ctx context.Context) error) {
  return nil
}, func (ctx context.Context) error) {
  return nil
}/*, ...*/)
// index: -1
// err: <nil>

index, err := async.All(func (ctx context.Context) error) {
  return nil
}, func (ctx context.Context) error) {
  return errors.New("some error")
}/*, ...*/)
// index: 1
// err: Some error

If you do not want to terminate the execution of other functions if some function returns an error or panic, you can try the AllCompleted function. The AllCompleted function will return until all functions are finished or panic. It'll return a list of the function return values (error), and a boolean value to indicate any functions return error or panic.

errors, ok := async.All(func (ctx context.Context) error) {
  return nil
}, func (ctx context.Context) error) {
  return errors.New("some error")
}/*, ...*/)
// errors: [<nil>, some error]
// ok: false

We also provided the Race function, it will return when a function returns or panics, and does not terminate other functions.

index, err := async.Race(func (ctx context.Context) error {
  request.Get("https://example.com")
  return nil
}, func (ctx context.Context) error {
  time.Sleep(time.Second)
  return nil
})
// index = 0 if the request is finished within one second.
// index = 1 if the request is finished after one second.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(funcs ...func(context.Context) error) (int, error)

All executes the functions asynchronously until all functions have been finished. If some function returns an error or panic, it will immediately return the index of the function and the error, and send a cancel signal to all other functions by context.

The index of the function will be -1 if all functions have been completed without error or panic.

func AllCompleted

func AllCompleted(funcs ...func(context.Context) error) ([]error, bool)

AllCompleted executes the functions asynchronously until all functions have been finished. It will return an error slice that is ordered by the functions order, and a boolean value to indicate whether any functions return an error or panic.

func AllCompletedWithContext

func AllCompletedWithContext(
	ctx context.Context,
	funcs ...func(context.Context) error,
) ([]error, bool)

AllCompletedWithContext executes the functions asynchronously until all functions have been finished, or the context is done (canceled or timeout). It will return an error slice that is ordered by the functions order, and a boolean value to indicate whether any functions return an error or panic.

func AllWithContext

func AllWithContext(ctx context.Context, funcs ...func(context.Context) error) (int, error)

AllWithContext executes the functions asynchronously until all functions have been finished, or the context is done (canceled or timeout). If some function returns an error or panic, it will immediately return the index of the index and the error and send a cancel signal to all other functions by context.

The index of the function will be -1 if all functions have been completed without error or panic, or the context has been canceled (or timeout) before all functions finished.

func Race

func Race(funcs ...func(context.Context) error) (int, error)

Race executes the functions asynchronously, it will return the index and the result of the first of the finished function (including panic), and it will not send a cancel signal to other functions.

func RaceWithContext

func RaceWithContext(ctx context.Context, funcs ...func(context.Context) error) (int, error)

RaceWithContext executes the functions asynchronously, it will return the index and the result of the first of the finished function (including panic), and it will not send a cancel signal to other functions.

Types

This section is empty.

Jump to

Keyboard shortcuts

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