diff --git a/example/http/http.go b/example/http/http.go index eaefb7f..93c9dd7 100644 --- a/example/http/http.go +++ b/example/http/http.go @@ -3,8 +3,9 @@ package main import ( "context" "fmt" - "github.com/hoanguyenkh/promise4g" "time" + + "github.com/hoanguyenkh/promise4g" ) type httpResponse1 struct { @@ -37,24 +38,14 @@ func fakeHttp2(url string) (httpResponse2, error) { func main() { ctx := context.Background() - p1 := promise4g.New(func(resolve func(any), reject func(error)) { - resp1, err := fakeHttp1("fakeHttp1") - if err != nil { - reject(err) - } else { - resolve(resp1) - } + t1 := time.Now() + p1 := promise4g.AsyncTask(func() (any, error) { + return fakeHttp1("fakeHttp1") }) - p2 := promise4g.New(func(resolve func(any), reject func(error)) { - resp1, err := fakeHttp2("fakeHttp2") - if err != nil { - reject(err) - } else { - resolve(resp1) - } + p2 := promise4g.AsyncTask(func() (any, error) { + return fakeHttp2("fakeHttp2") }) - p := promise4g.All(ctx, p1, p2) results, err := p.Await(ctx) if err != nil { @@ -65,4 +56,7 @@ func main() { res2 := results[1].(httpResponse2) fmt.Println(res1.RequestId, res1.Message) fmt.Println(res2.RequestId, res2.Username) + + fmt.Println("total time: ", time.Since(t1)) + } diff --git a/promise.go b/promise.go index 9a1a21f..cde85a9 100644 --- a/promise.go +++ b/promise.go @@ -206,3 +206,16 @@ func Timeout[T any](p *Promise[T], d time.Duration) *Promise[T] { } }, defaultPool) } + +// AsyncTask creates a new Promise that executes the provided function asynchronously. +// It resolves with the function's result or rejects with an error if the function fails. +func AsyncTask[T any](fn func() (T, error)) *Promise[T] { + return New(func(resolve func(T), reject func(error)) { + resp, err := fn() + if err != nil { + reject(err) + } else { + resolve(resp) + } + }) +}