@@ -2,56 +2,128 @@ package main
22
33import (
44 "context"
5+ "errors"
6+ "fmt"
57 "log"
68 "time"
79
810 "github.com/reugn/async"
11+ "github.com/reugn/async/internal/ptr"
912)
1013
11- const ok = "OK"
12-
1314func main () {
14- // using a promise
15- future1 := asyncAction ()
16- result1 , err := future1 .Join ()
15+ runPromiseExample ()
16+ runTaskExample ()
17+ runExecutorExample ()
18+ runTransformationsExample ()
19+ runRecoveryExample ()
20+ }
21+
22+ func runPromiseExample () {
23+ fmt .Println ("=== Promise Example ===" )
24+ future := asyncAction ()
25+ result , err := future .Join ()
1726 if err != nil {
1827 log .Fatal (err )
1928 }
20- log .Print (result1 )
29+ fmt .Printf ("Result: %s\n " , result )
30+ }
2131
22- // using a task
23- task := async .NewTask (func () (string , error ) { return ok , nil })
24- result2 , err := task .Call ().Join ()
32+ func runTaskExample () {
33+ fmt .Println ("\n === Task Example ===" )
34+ task := async .NewTask (func () (string , error ) {
35+ time .Sleep (100 * time .Millisecond )
36+ return "Task completed" , nil
37+ })
38+ result , err := task .Call ().Join ()
2539 if err != nil {
2640 log .Fatal (err )
2741 }
28- log .Print (result2 )
42+ fmt .Printf ("Result: %s\n " , result )
43+ }
2944
30- // using the executor
45+ func runExecutorExample () {
46+ fmt .Println ("\n === Executor Example ===" )
3147 ctx := context .Background ()
3248 executor := async .NewExecutor [* string ](ctx , async .NewExecutorConfig (2 , 2 ))
3349
34- future3 , err := executor .Submit (func (_ context.Context ) (* string , error ) {
35- value := ok
36- return & value , nil
50+ future , err := executor .Submit (func (_ context.Context ) (* string , error ) {
51+ value := "Executor task completed"
52+ return ptr . Of ( value ) , nil
3753 })
54+ if err != nil {
55+ if shutdownErr := executor .Shutdown (); shutdownErr != nil {
56+ log .Printf ("Error shutting down executor: %v" , shutdownErr )
57+ }
58+ log .Fatal (err )
59+ }
60+
61+ result , err := future .Get (ctx )
62+ if err != nil {
63+ if shutdownErr := executor .Shutdown (); shutdownErr != nil {
64+ log .Printf ("Error shutting down executor: %v" , shutdownErr )
65+ }
66+ log .Fatal (err )
67+ }
68+ fmt .Printf ("Result: %s\n " , * result )
69+
70+ if err := executor .Shutdown (); err != nil {
71+ log .Printf ("Error shutting down executor: %v" , err )
72+ }
73+ }
74+
75+ func runTransformationsExample () {
76+ fmt .Println ("\n === Future Transformations ===" )
77+ promise := async .NewPromise [int ]()
78+ go func () {
79+ time .Sleep (50 * time .Millisecond )
80+ promise .Success (10 )
81+ }()
82+
83+ transformed := promise .Future ().
84+ Map (func (v int ) (int , error ) {
85+ return v * 2 , nil
86+ }).
87+ FlatMap (func (v int ) (async.Future [int ], error ) {
88+ p := async .NewPromise [int ]()
89+ go func () {
90+ time .Sleep (50 * time .Millisecond )
91+ p .Success (v + 5 )
92+ }()
93+ return p .Future (), nil
94+ })
95+
96+ result , err := transformed .Join ()
3897 if err != nil {
3998 log .Fatal (err )
4099 }
100+ fmt .Printf ("Transformed result: %d\n " , result )
101+ }
102+
103+ func runRecoveryExample () {
104+ fmt .Println ("\n === Error Recovery ===" )
105+ failingPromise := async .NewPromise [int ]()
106+ go func () {
107+ time .Sleep (50 * time .Millisecond )
108+ failingPromise .Failure (errors .New ("operation failed" ))
109+ }()
41110
42- result3 , err := future3 .Get (ctx )
111+ recovered := failingPromise .Future ().Recover (func () (int , error ) {
112+ return 10 , nil // fallback value
113+ })
114+
115+ result , err := recovered .Join ()
43116 if err != nil {
44117 log .Fatal (err )
45118 }
46- log . Print ( * result3 )
119+ fmt . Printf ( "Recovered result: %d \n " , result )
47120}
48121
49122func asyncAction () async.Future [string ] {
50123 promise := async .NewPromise [string ]()
51124 go func () {
52- time .Sleep (time .Second )
53- promise .Success (ok )
125+ time .Sleep (100 * time .Millisecond )
126+ promise .Success ("Promise completed" )
54127 }()
55-
56128 return promise .Future ()
57129}
0 commit comments