package
module
Version:
v0.0.0-...-92edfa0
Opens a new window with list of versions in this module.
Published: May 12, 2025
License: MIT
Opens a new window with license information.
Imports: 0
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
Future
A Future is a basic primitive for managing future results of any type. The result can be set once but retrieved multiple times.
Example
package main
import (
"fmt"
"github.com/gavraz/async/future"
"time"
)
func makePromise() *future.Future[int] {
p := future.NewPromise[int]()
go func() {
time.Sleep(2 * time.Second)
p.Set(42)
}()
return p.Future()
}
func main() {
f := makePromise()
go func() {
select {
// ...
case <-f.Done():
}
fmt.Println("Done waiting for result")
}()
fmt.Println("Result:", f.Value())
// output:
// Done waiting for result
// Result: 42
}
Documentation
¶
type Future[T any] struct {
}
Future is used to wait for a value that may be available in the future.
Callers may use Done to wait in a select statement.
func (f *Future[T]) Done() <-chan struct{}
Done is a channel used in select statements to wait until the value is available.
Value blocks until the value is set and then returns it.
If the result is already set, it returns immediately.
type Promise[T any] struct {
}
Promise is used to commit for value.
The promised value can be set using Set and be retrieved using Future.
Set assigns a value to the future and makes it available to waiting routines.
The result can be set only once.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.