Giter VIP home page Giter VIP logo

async's Introduction

async

This is an implement of async/await by go. It uses generic (>= go1.18) to get returned values without any type asserts.

Here 10 patterns are supported:

  • Exec: asynchronously executes a function

  • ExecReturnOne(alias ExecOne): asynchronously executes a function which returns one value

  • ExecReturnTwo(alias ExecTwo): asynchronously executes a function which returns two value

  • ExecReturnThree(alias ExecThree): asynchronously executes a function which returns three value

  • ExecReturnFour(alias ExecFour): asynchronously executes a function which returns four value

  • ExecAll: asynchronously executes multiple functions

  • ExecAllReturnOne(alias ExecAllOne): asynchronously executes multiple functions which return one value

  • ExecAllReturnTwo(alias ExecAllTwo): asynchronously executes multiple functions which return two value

  • ExecAllReturnThree(alias ExecAllThree): asynchronously executes multiple functions which return three value

  • ExecAllReturnFour(alias ExecAllFour): asynchronously executes multiple functions which return four value

And these functions can continue with Await(ctx) to wait goroutines done and returning values.

This is especially useful when you want to run multiple goroutines and wait to get there returned values at the same time, just like:

package main

import (
	"context"
	"fmt"
	"strconv"

	"github.com/Decmoe47/async"
)

func main() {
	ctx := context.Background()
	
	var fns []func() (int, string)
	for i := 0; i < 5; i++ {
		i := i
		fns = append(fns, func() (int, string) {
			fmt.Println("executes function ", i)
			return i, strconv.Itoa(i + 5)
		})
	}
	res1, res2 := async.ExecAllTwo(ctx, fns).Await(ctx)
	fmt.Println("all functions done! result: ", res1, res2)
}

inspired from Joker666/AsyncGoDemo

async's People

Contributors

decmoe47 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.