futures

package
v0.0.0-...-b14404a Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Join

func Join[T any](ctx context.Context, futures ...Func[T]) []result[T]

func JoinN

func JoinN[T any](ctx context.Context, n int64, futures ...Func[T]) []result[T]

Types

type Func

type Func[T any] func(ctx context.Context) (T, error)
Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/alextanhongpin/core/sync/futures"
)

func main() {
	f0 := futures.Func[int](func(ctx context.Context) (int, error) {
		time.Sleep(100 * time.Millisecond)
		return 42, nil
	})
	f1 := futures.Func[int](func(ctx context.Context) (int, error) {
		time.Sleep(100 * time.Millisecond)
		return 3173, nil
	})

	now := time.Now()
	res := futures.Join(context.Background(), f0, f1)
	isWithinSLA := time.Since(now) < 110*time.Millisecond

	meaningOfLife, err := res[0].Unwrap()
	if err != nil {
		panic(err)
	}

	elie, err := res[1].Unwrap()
	if err != nil {
		panic(err)
	}

	fmt.Println(isWithinSLA)
	fmt.Println(meaningOfLife)
	fmt.Println(elie)
	// Output
	// true
	// 42
	// 3173
}
Output:

func (Func[T]) Await

func (f Func[T]) Await(ctx context.Context) result[T]

Jump to

Keyboard shortcuts

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