1- # Async (Experimental)
1+ # Fillmore Labs Async (Experimental)
22
33[ ![ Go Reference] ( https://pkg.go.dev/badge/fillmore-labs.com/exp/async.svg )] ( https://pkg.go.dev/fillmore-labs.com/exp/async )
44[ ![ Build Status] ( https://badge.buildkite.com/06fc8f7bdcfc5c380ea0c7c8bb92a7cee8b1676b841f3c65c8.svg )] ( https://buildkite.com/fillmore-labs/async-exp )
5+ [ ![ GitHub Workflow] ( https://github.com/fillmore-labs/exp-async/actions/workflows/test.yml/badge.svg?branch=main )] ( https://github.com/fillmore-labs/async-exp/actions/workflows/test.yml )
56[ ![ Test Coverage] ( https://codecov.io/gh/fillmore-labs/async-exp/graph/badge.svg?token=GQUJA8PKJI )] ( https://codecov.io/gh/fillmore-labs/async-exp )
67[ ![ Maintainability] ( https://api.codeclimate.com/v1/badges/72fe9626fb821fc70251/maintainability )] ( https://codeclimate.com/github/fillmore-labs/async-exp/maintainability )
78[ ![ Go Report Card] ( https://goreportcard.com/badge/fillmore-labs.com/exp/async )] ( https://goreportcard.com/report/fillmore-labs.com/exp/async )
8- [ ![ License] ( https://img.shields.io/github/license/fillmore-labs/exp-async )] ( https://github.com/fillmore-labs/exp-async/blob/main/ LICENSE )
9+ [ ![ License] ( https://img.shields.io/github/license/fillmore-labs/exp-async )] ( https://www.apache.org/licenses/ LICENSE-2.0 )
910
1011The ` async ` package provides interfaces and utilities for writing asynchronous code in Go.
1112
@@ -36,10 +37,10 @@ address (see [GetMyIP](#getmyip) for an example).
3637Now you can do
3738
3839``` go
39- ctx , cancel := context.WithTimeout (context.Background (), timeout )
40+ ctx , cancel := context.WithTimeout (context.Background (), 5 *time. Second )
4041 defer cancel ()
4142
42- future := async.NewAsyncFuture (func () (string , error ) {
43+ future := async.NewFutureAsync (func () (string , error ) {
4344 return getMyIP (ctx)
4445 })
4546```
@@ -48,9 +49,9 @@ and elsewhere in your program, even in a different goroutine
4849
4950``` go
5051 if ip , err := future.Wait (ctx); err == nil {
51- log. Printf ( " My IP is %s " , ip)
52+ slog. Info ( " Found IP" , " ip " , ip)
5253 } else {
53- log. Printf ( " Error fetching IP: %v " , err)
54+ slog. Error ( " Failed to fetch IP " , " error " , err)
5455 }
5556```
5657
@@ -61,53 +62,30 @@ decoupling query construction from result processing.
6162Sample code to retrieve your IP address:
6263
6364``` go
64- const (
65- serverURL = " https://httpbin.org/ip"
66- timeout = 2 * time.Second
67- )
68-
69- type IPResponse struct {
70- Origin string ` json:"origin"`
71- }
65+ const serverURL = " https://httpbin.org/ip"
7266
7367func getMyIP (ctx context .Context ) (string , error ) {
74- resp , err := sendRequest (ctx)
68+ req , err := http. NewRequestWithContext (ctx, http. MethodGet , serverURL, nil )
7569 if err != nil {
7670 return " " , err
7771 }
72+ req.Header .Set (" Accept" , " application/json" )
7873
79- ipResponse , err := decodeResponse (resp )
74+ resp , err := http. DefaultClient . Do (req )
8075 if err != nil {
8176 return " " , err
8277 }
78+ defer func () { _ = resp.Body .Close () }()
8379
84- return ipResponse.Origin , nil
85- }
80+ ipResponse := &struct {
81+ Origin string ` json:"origin"`
82+ }{}
8683
87- func sendRequest (ctx context .Context ) (*http .Response , error ) {
88- req , err := http.NewRequestWithContext (ctx, http.MethodGet , serverURL, nil )
89- if err != nil {
90- return nil , err
91- }
92- req.Header .Set (" Accept" , " application/json" )
93-
94- return http.DefaultClient .Do (req)
95- }
96-
97- func decodeResponse (response *http .Response ) (*IPResponse , error ) {
98- body , err := io.ReadAll (response.Body )
99- _ = response.Body .Close ()
100- if err != nil {
101- return nil , err
102- }
103-
104- ipResponse := &IPResponse{}
105- err = json.Unmarshal (body, ipResponse)
106- if err != nil {
107- return nil , err
84+ if err := json.NewDecoder (resp.Body ).Decode (ipResponse); err != nil {
85+ return " " , err
10886 }
10987
110- return ipResponse, nil
88+ return ipResponse. Origin , nil
11189}
11290```
11391
0 commit comments