Promise Package
Package promise provides a robust implementation of Promises in Go, enabling asynchronous programming patterns similar to those found in JavaScript.
Features:
Asynchronous operations with resolution and rejection.
Chaining and composition of Promises.
Cancellation support.
Timeouts and context integration.
Utility functions like All and Race.
Usage Example
p := promise.NewPromise[int]()
p.Then(func(result int) interface{} {
fmt.Println("Result:", result)
return nil
}).Catch(func(err error) interface{} {
fmt.Println("Error:", err)
return nil
})
go func() {
time.Sleep(2 * time.Second)
p.Resolve(42)
}()
result, err := p.Await()
if err != nil {
log.Fatal(err)
}
fmt.Println("Awaited Result:", result)
Last modified: 28 January 2025