modulepackage
0.3.0
Repository: https://github.com/allan-jacobs/go-futures.git
Documentation: pkg.go.dev
# README
Go-Futures
An easy to use generic future implementation in Go.
Install
go get github.com/Allan-Jacobs/go-futures@latest
Example
package main
import (
"fmt"
"net/http"
"github.com/Allan-Jacobs/go-futures/futures"
)
// HTTPGetAsync wraps http.Get into a future based api
func HTTPGetAsync(url string) futures.Future[*http.Response] {
return futures.GoroutineFuture(func() (*http.Response, error) {
return http.Get(url)
})
}
func main() {
// run futures simultaneously and await aggregated results
responses, err := futures.All(
HTTPGetAsync("https://go.dev"),
HTTPGetAsync("https://pkg.dev"),
).Await()
if err != nil {
fmt.Println("Error:", err)
}
for _, res := range responses {
fmt.Println(res.Request.URL, res.Status)
}
}
License
go-futures is MIT Licensed
# Functions
All settles to a slice of results when all the futures resolve, or returns an error with any futures that reject.
No description provided by the author
Any settles to the first future to resolve, or rejects with an error aggregation if none resolve.
This runs f in a goroutine and returns a future that settles the the result of f.
Chain takes a Future[T] and returns a future that is mapped with mapper, propagating any errors that occur.
Chain takes a Future[T] and returns a future that is mapped with mapper, passing any errors that occurred to mapper.
A future that resolves to the next value of the channel.
Returns a Completer and a Future.
Returns a future that resolves the same value as the inner future, unless the outer future errors, in which case returned the future errors.
This runs f in a goroutine and returns a future that settles the the result of f.
OnReject takes a Future[T] and executes the callback in another goroutine if the future rejects.
OnResolve takes a Future[T] and executes the callback in another goroutine if the future resolves.
OnSettled takes a Future[T] and executes the callback in another goroutine when it is settled.
A future type similar to JavaScript's Promise This runs f in a different goroutine, so be aware of potential race conditions.
Race settles to the first future to finish.
Returns a future that rejects with err.
Returns a future that resolves to value.
A future that resolves after the duration.
Returns a Future that times out with ErrTimeout if future does not settle before timeout.
# Variables
An error that happens when a future gets cancelled.
An error happens when ChannelFuture is called on a closed channel.
An error happens when ChannelFuture is called on a nil channel.
An error that happens when a future times out.
# Interfaces
A future that can be cancelled.
An aggregate error type.
A Future[T] represents an asynchronous operation.