task

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

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

func AwaitAll[T any](tasks []*Task[T]) ([]T, error)

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

func Map[T, R any](t *Task[T], fn func(T) R) *Task[R]

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

func Run[T any](fn func() (T, error)) *Task[T]

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

func (*Task[T]) Await

func (t *Task[T]) Await() (T, error)

Await blocks until the task is complete and returns the value and any error. It is safe to call Await multiple times or from multiple goroutines.

func (*Task[T]) MustAwait

func (t *Task[T]) MustAwait() T

MustAwait blocks until the task is complete and returns the value. It panics if the task returned an error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL