A ES7-style async/await implementation lib based on Goroutine + Channel in Golang.
$ go get github.com/sun0day/asyncAsync 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.AsyncTaskis doing)async.FULFILLED(async.AsyncTaskis done and result returns)async.REJECTED(async.AsyncTaskis done but error happens).
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*/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*/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*/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*/