You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

d.zyszy.best/ghosind/go-async

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

d.zyszy.best/ghosind/go-async

v1.0.1
Go
Version published
Created
Source

go-async

test Go Report Card codecov Version Badge License Badge Go Reference

English | 简体中文

It's a powerful asynchronous utility library inspired by JavaScript Promise Object and Node.js async package.

Installation and Requirement

Run the following command to install this library, and Go 1.18 and later versions required.

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

And then, import the library into your own code.

import "github.com/ghosind/go-async"

This library is not stable yet, anything may change in the later versions.

Getting Started

For the following example, it runs the functions concurrently and returns the return values until all functions have been completed.

out, err := async.All(func (ctx context.Context) (int, error) {
  return 0, nil
}, func () (string, error)) {
  time.Sleep(100 * time.Millisecond)
  return "hello", nil
})
// out: [][]any{{0, <nil>}, {"hello", <nil>}}
// err: <nil>

There are over 10 asynchronous control flow functions available, please visit Go Reference to see the documentation and examples.

The function to run

The most of utility functions of this library accept any type of function to run, you can set the parameters and the return values as any type and any number of return values that you want. However, for best practice, we recommend you to set the first parameter as context.Context to receive the signals and make the type of the last return value as an error to let the utilities know whether an error happened or not.

Customize context

For all functions, you can use the XXXWithContext function (like AllWithContext, RaceWithContext, ...) to set the context by yourself.

Available Functions

License

The library published under MIT License, please see license file for more details.

FAQs

Package last updated on 09 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts