Documentation
¶
Overview ¶
Package task provides typed asynchronous work units inspired by Elixir's Task module. Run starts a function in a goroutine; Await retrieves the result, blocking until it is ready.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AwaitAll ¶
AwaitAll waits for all tasks and returns their values in order. The first error encountered is returned; remaining tasks continue running.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/task"
)
func main() {
tasks := []*task.Task[int]{
task.Run(func() (int, error) { return 1, nil }),
task.Run(func() (int, error) { return 2, nil }),
task.Run(func() (int, error) { return 3, nil }),
}
results, _ := task.AwaitAll(tasks)
fmt.Println(results)
}
Output: [1 2 3]
Types ¶
type Task ¶
type Task[T any] struct { // contains filtered or unexported fields }
Task represents an asynchronous computation of type T. A Task is safe to Await from multiple goroutines; all callers receive the same result.
func Map ¶
Map starts a new Task that applies fn to the result of t when it completes. If t fails, the mapped task propagates the error without calling fn. This is a package-level function because Go methods cannot introduce additional type parameters.
func Run ¶
Run starts fn in a new goroutine and returns a Task to retrieve the result.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/task"
)
func main() {
t := task.Run(func() (int, error) { return 42, nil })
v, _ := t.Await()
fmt.Println(v)
}
Output: 42