Skip to content

Latest commit

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

async

A ES7-style async/await implementation lib based on Goroutine + Channel in Golang.

Install

$ go get github.com/sun0day/async

API

async.AsyncTask

Async work unit created by async.Async.

typeAsyncTask[Vany] struct {
ValueV// execution resultErrany// execution possible errorStateAsyncState// execution state
}

async.AsyncTask.State has 3 values:

  • async.PENDING(async.AsyncTask is doing)
  • async.FULFILLED(async.AsyncTask is done and result returns)
  • async.REJECTED(async.AsyncTask is done but error happens).

async.Async

Convert a synchronous func to be asynchronous.

Type definition

funcAsync[Vany](ffunc() V) func() *AsyncTask[V] {...}

Usage

package main
import (
"fmt""time""github.com/sun0day/async"
)
funcmain() {
a:=1b:=2f:=func() int {
c:=a+bfmt.Printf("async result=%d\n", c)
returnc
}
af:= async.Async[int](f)
fmt.Printf("sync start, goroutine=%d\n", runtime.NumGoroutine())
af()
fmt.Printf("sync end, goroutine=%d\n", runtime.NumGoroutine())
time.Sleep(1*time.Second)
fmt.Printf("async end, goroutine=%d\n", runtime.NumGoroutine())
}
/* stdoutsync start, goroutine=1sync end, goroutine=2async result=3async end, goroutine=1*/

async.Await

Wait async.AsyncTask to be done.

Type definition

funcAwait[Vany](t*AsyncTask[V]) (V, any) {...}

Usage

package main
import (
"fmt""runtime""github.com/sun0day/async"
)
funcmain() {
a:=1b:=2f1:=func() int {
c:=a+breturnc
}
f2:=func() int {
panic("f() error")
}
af1:= async.Async[int](f1)
af2:= async.Async[int](f2)
fmt.Printf("sync start, goroutine=%d\n", runtime.NumGoroutine())
value, _:= async.Await[int](af1())
_, err:= async.Await[int](af2())
fmt.Printf("af1 result=%d\n", value)
fmt.Printf("af2 error=%s\n", err)
fmt.Printf("sync end, goroutine=%d\n", runtime.NumGoroutine())
}
/* stdoutsync start, goroutine=1af1 result=3af2 error=f() errorsync end, goroutine=1*/

async.All

Asynchronously return values(when all funcs are finished) or error(once one of funcs throws error).

Type definition

funcAll[Vany](fs []func() V) *AsyncTask[[]V] {...}

Usage

package main
import (
"fmt""runtime""time""github.com/sun0day/async"
)
funcmain() {
a:=1b:=2f1:=func() int {
time.Sleep(1*time.Second)
c:=a+breturnc
}
f2:=func() int {
time.Sleep(2*time.Second)
c:=a*breturnc
}
f3:=func() int {
panic("f3 error")
}
fmt.Printf("sync start, goroutine=%d\n", runtime.NumGoroutine())
values, _:= async.Await[[]int](async.All[int]([]func() int{f1, f2}))
_, err:= async.Await[[]int](async.All[int]([]func() int{f1, f2, f3}))
fmt.Printf("all result=%v\n", values)
fmt.Printf("all error=%s\n", err)
fmt.Printf("sync end, goroutine=%d\n", runtime.NumGoroutine())
}
/* stdoutsync start, goroutine=1all result=[3 2]all error=f3 errorsync end, goroutine=1*/

async.Race

Asynchronously return value or error of the fisrt finished func.

Type definition

funcRace[Vany](fs []func() V) *AsyncTask[V] {...}

Usage

package main
import (
"fmt""runtime""time""github.com/sun0day/async"
)
funcmain() {
a:=1b:=2f1:=func() int {
time.Sleep(1*time.Second)
c:=a+breturnc
}
f2:=func() int {
time.Sleep(2*time.Second)
c:=a*breturnc
}
fmt.Printf("sync start, goroutine=%d\n", runtime.NumGoroutine())
value, _:= async.Await[int](async.Race[int]([]func() int{f1, f2}))
fmt.Printf("race result=%d\n", value)
fmt.Printf("sync end, goroutine=%d\n", runtime.NumGoroutine())
}
/* stdoutsync start, goroutine=1race result=3sync end, goroutine=2*/

About

A ES7-style async/await implementation in Golang

Resources

Stars

17 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages