@@ -12,22 +12,8 @@ The `async` package provides interfaces and utilities for writing asynchronous c
12
12
13
13
## Motivation
14
14
15
- Futures and promises are constructs used for asynchronous and concurrent programming, allowing developers to work with
16
- values that may not be immediately available and can be evaluated in a different execution context.
17
-
18
- Go is known for its built-in concurrency features like goroutines and channels.
19
- The select statement further allows for efficient multiplexing and synchronization of multiple channels, thereby
20
- enabling developers to coordinate and orchestrate asynchronous operations effectively.
21
- Additionally, the context package offers a standardized way to manage cancellation, deadlines, and timeouts within
22
- concurrent and asynchronous code.
23
-
24
- On the other hand, Go's error handling mechanism, based on explicit error values returned from functions, provides a
25
- clear and concise way to handle errors.
26
-
27
- The purpose of this package is to provide a thin layer over channels which simplifies the integration of concurrent
28
- code while providing a cohesive strategy for handling asynchronous errors.
29
- By adhering to Go's standard conventions for asynchronous and concurrent code, as well as error propagation, this
30
- package aims to enhance developer productivity and code reliability in scenarios requiring asynchronous operations.
15
+ This is an experimental package which has a similar API as
16
+ [ fillmore-labs.com/promise] ( https://pkg.go.dev/fillmore-labs.com/promise ) , but is implemented with a structure instead.
31
17
32
18
## Usage
33
19
@@ -37,18 +23,19 @@ address (see [GetMyIP](#getmyip) for an example).
37
23
Now you can do
38
24
39
25
``` go
40
- ctx , cancel := context.WithTimeout (context.Background (), 5 *time.Second )
26
+ ctx , cancel := context.WithTimeout (context.Background (), 2 *time.Second )
41
27
defer cancel ()
42
28
43
- future := async.NewFutureAsync (func () (string , error ) {
44
- return getMyIP (ctx)
45
- })
29
+ query := func () (string , error ) {
30
+ return getMyIP (ctx) // Capture context with timeout
31
+ }
32
+ future := async.NewAsync (query)
46
33
```
47
34
48
35
and elsewhere in your program, even in a different goroutine
49
36
50
37
``` go
51
- if ip , err := future.Wait (ctx); err == nil {
38
+ if ip , err := future.Await (ctx); err == nil {
52
39
slog.Info (" Found IP" , " ip" , ip)
53
40
} else {
54
41
slog.Error (" Failed to fetch IP" , " error" , err)
@@ -77,27 +64,15 @@ func getMyIP(ctx context.Context) (string, error) {
77
64
}
78
65
defer func () { _ = resp.Body .Close () }()
79
66
80
- ipResponse := & struct {
67
+ ipResponse := struct {
81
68
Origin string ` json:"origin"`
82
69
}{}
70
+ err = json.NewDecoder (resp.Body ).Decode (&ipResponse)
83
71
84
- if err := json.NewDecoder (resp.Body ).Decode (ipResponse); err != nil {
85
- return " " , err
86
- }
87
-
88
- return ipResponse.Origin , nil
72
+ return ipResponse.Origin , err
89
73
}
90
74
```
91
75
92
- ## Concurrency Correctness
93
-
94
- When utilizing plain Go channels for concurrency, reasoning over the correctness of concurrent code becomes simpler
95
- compared to some other implementations of futures and promises.
96
- Channels provide a clear and explicit means of communication and synchronization between concurrent goroutines, making
97
- it easier to understand and reason about the flow of data and control in a concurrent program.
98
-
99
- Therefore, this library provides a straightforward and idiomatic approach to achieving concurrency correctness.
100
-
101
76
## Links
102
77
103
78
- [ Futures and Promises] ( https://en.wikipedia.org/wiki/Futures_and_promises ) in the English Wikipedia
0 commit comments