Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

async

> 编程语言
Open source

Async utilities for Golang.

100 stars0 likes0 views
WebsiteGitHub

About

Async utilities for Golang.

async

async is a utility library for manipulating synchronous and asynchronous flows.

Install

bash
$ go get github.com/rafaeldias/async

Concurrent

go
async.Concurrent(tasks taskier) (Results, error)

Concurrent will execute all the functions concurrently using goroutines.

  • tasks is an internal interface which accept two async public types:
    • Tasks is a list of functions to be executed concurrently.
    • MapTasks is a map of string of functions to be executed concurrently.

The type of Results value depends on the type of tasks passed to the function. See type Results

All errors ocurred in the functions will be returned. See returning error and type Errors.

Parallel

go
async.Parallel(tasks taskier) (Results, error)

Parallel will execute all the functions in parallel. It creates multiple goroutines and distributes the functions execution among them. The number of goroutines defaults to GOMAXPROCS. If the number of active goroutines is equal to GOMAXPROCS and there're more functions to execute, these functions will wait until one of functions being executed finishes its job.

  • tasks is an internal interface which accept two async public types:
    • async.Tasks is a list of functions to be executed in parallel.
    • async.MapTasks is a map of string of functions to be executed in parallel.

The type of Results value depends on the type of tasks passed to the function. See type Results.

All errors ocurred in the functions will be returned. See returning error and type Errors.

Waterfall

go
async.Waterfall(tasks Tasks, args ...interface{}) ([]interface[}, error)

Waterfall will execute all the functions in sequence, each returning their results to the next. If the last returning value of the function is of type error, then this value will not be passed to the next function.

  • tasks is a list of functions that will be executed in series.
  • args are optional parameters that will be passed to the first task.

Waterfall returns the results of the last task as []interface{} and error.

If an error occur in any of the functions to be executed, the next function will not be executed, and the error will be returned to the caller. See returning error.

Type Results

The Results is the type that is returned by Parallel and Concurrent:

go
type Results interface {
      Index(int) []interface{}  // Gets values by index
      Key(string) []interface{} // Gets values by key
      Len() int                 // Gets the length of the results
      Keys() []string           // Gets the keys of the results
}

The underlying type of Results will be different depending on the type of tasks passed to either Parallel or Concurrent. There are two async public types that can be passed to both functions:

  • Tasks is a list of functions that will be executed.
  • MapTasks is a map of string of functions to be executed.

When using Tasks, the underlying Results will be [][]interface{}:

go
res, err : = async.Parallel(async.Tasks{
        func1,
        func2,
        ...funcN,
})

We can get the results of the the second function:

go
res.Index(1)

Or we can iterate over the results using the Len() method:

go
for i := 0; i  0 {
                    // This line will interrupt the execution flow
                    return "", errors.New("Error occurred")
                }
                return "Ok", nil
        },
        // This function will not be executed.
        func (s string) {
            return
        }
});

if err != nil {
      fmt.Println(err.Error()); // "Error occurred"
}

Examples

Waterfall

go
import (
        "fmt"

        "github.com/rafaeldias/async"
)

func fib(p, c int) (int, int) {
  return c, p + c
}

func main() {

        // execution in series.
        res, e := async.Waterfall(async.Tasks{
                fib,
                fib,
                fib,
                func(p, c int) (int, error) {
                        return c, nil
                },
        }, 0, 1)

        if e != nil {
              fmt.Printf("Error executing a Waterfall (%s)\n", e.Error())
        }

        fmt.Println(res[0].(int)) // Prints 3
}

Parallel

go
import (
        "fmt"
        "time"

        "github.com/rafaeldias/async"
)

func main() {

        res, e := async.Parallel(async.MapTasks{
                "one": func() int {
                        for i := 'a'; i = 'a'; i-- {
                                fmt.Printf("%c ", i)
                        }
                        
                        return 3
                },
        })

        if e != nil {
                fmt.Printf("Errors [%s]\n", e.Error())
        }
        
        fmt.Println("Results from task 'two': %v", res.Key("two"))
}

Concurrent

…

License

Distributed under MIT License. See LICENSE file for more details.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Goasyncconcurrencygoroutineparallel

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言